jsp页面代码l:
<form id="createOrderForm" action="orderController/createOrder">
<c:forEach items="${order.orderItemList }" var="orderItem" varStatus="status">
<div>
<input type="hidden" name="orderItemList[${status.index }].setMeal.id" value="${orderItem.setMeal.id }">
<input type="hidden" name="orderItemList[${status.index }].quantity" value="${orderItem.quantity }">
<input type="hidden" name="orderItemList[${status.index }].type" value="${orderItem.type }">
</div>
</c:forEach>
</form>
在后台java代码中,通过直接写下面代码,不可行
private List<OrderItem> orderItemList = new ArrayList<OrderItem>();
public List<OrderItem> getOrderItemList() {
return orderItemList;
}
public void setOrderItemList(List<OrderItem> orderItemList) {
this.orderItemList = orderItemList;
}
form表单中的name对应不到list集合中
解决方法:
写一个辅助类,将该list放在辅助类中,
public class HelperClass {
private List<OrderItem> orderItemList = new ArrayList<OrderItem>();
public List<OrderItem> getOrderItemList() {
return orderItemList;
}
public void setOrderItemList(List<OrderItem> orderItemList) {
this.orderItemList = orderItemList;
}
}
方法代码:
@RequestMapping(value = { "/createOrder" }, method = RequestMethod.GET)
public String createOrder(@ModelAttribute("form") HelperClass helper){
List<OrderItem> orderItemList = helper.getOrderItemList();
}