我想使用ModelAttribute绑定List。该列表包含Transaction类型的对象,每个对象都包含transactionid(类型int)。这是我的控制器代码:
@RequestMapping(value = "/approvecreditdebit.do", method = RequestMethod.POST)
public ModelAndView doActions(HttpServletRequest request,
@ModelAttribute("clc") Clc transactionList, BindingResult result,
ModelMap model) {
/*
* switch (action) { case "approve":
*/
System.out.println("Obj = " + transactionList.getClass());
System.out.println("Val = " + transactionList.getTransactionList());
Users users = new Users();
return new ModelAndView("internal","internal",users);
}
这是我的jsp代码:
<form:form action="approvecreditdebit.do" method="POST"
modelAttribute="clc">
<table border="1">
<tr>
<th>no</th>
<th>ID</th>
</tr>
<c:forEach items="${clc.transactionList}"
var="transaction" varStatus="status">
<tr>
<td>${status.index}</td>
<td><input name = "transaction[${status.index}].transactionId"
value="${transaction.transactionId}" /></td>
</tr>
</c:forEach>
</table>
<br>
<br>
<center>
<input type="submit" value="approve" />
</center>
</form:form>
这是Clc类:
public class Clc{
private List<Transaction> transactionList;
public List<Transaction> getTransactionList() {
return transactionList;
}
public void setTransactionList(List<Transaction> transactionList) {
this.transactionList = transactionList;
}
}
transactionList的值未设置为从表单接收的值。我收到以下错误:
Request processing failed; nested exception is java.lang.NullPointerException
我尝试在google上搜索解决方案并从stackoverflow获得了很多解决方案,但它们似乎都无法正常工作。
答案 0 :(得分:0)
尝试这样的事情(注意使用<form:input>
)。我只是在一个简单的Spring MVC应用程序上尝试它并且它可以工作(当我尝试在我的POST方法中访问它时,list不是null并且具有表单中的值)。
<c:forEach var="transaction" varStatus="status" items="${clc.transactionList}">
<tr>
<td>${status.index}</td>
<td><form:input path="transactionList[${status.index}].id" /></td>
</tr>
</c:forEach>