0
我想使用ModelAttribute來綁定列表。該列表包含Transaction類型的對象,每個對象都包含transactionid(int類型)。這是我的控制器代碼:ModelAttribute在春天不能使用列表
@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
我試圖尋找對谷歌的解決方案,並得到了很多來自計算器的解決方案,但他們都不工作。
太棒了!我不知道它是如何工作的,因爲我之前嘗試過使用form:input。很多解決方案都表明這一點。但是名單仍然是空的。唯一的區別在於forEach變量的順序。我不明白這會如何影響結果。非常感謝! – 2014-11-05 05:55:34