我有一个.jsp
页面是使用我的"menuModel"
生成的,现在可视化此页面后,用户会插入他/她的名字并点击Confirm My Order
按钮。
点击此按钮,我想将此menuModel
的数据复制到名为“ordersModel”的另一个模型。由于此ordersModel
未实例化,我不知道如何将menuModel
的数据复制到ordersModel
。
<form:form id="myOrderMenuId" action="${pageContext.request.contextPath}/OrderSent" method="post" commandName="ordersModel" >
<c:forEach items="#{menuModel.foodList}" var="foodModel" varStatus="status">
<c:if test="${foodModel.quantity != 0}">
<!-- <form:hidden value='${foodModel.foodName}' path='orders[${status.index}].foodName'/>
<form:hidden value='${foodModel.quantity}' path='orders[${status.index}].quantity'/> -->
<div>
<table class="itemList">
<tr>
<td style="width: 170px">
<span id="foodNameId${foodModel.foodId}"><c:out value="${foodModel.foodName}"/></span>
</td>
<td>
<span id="quantityId${foodModel.foodId}" style="margin-left: 110px;"><c:out value="${foodModel.quantity}"/></span>
</td>
<td>
<span id="priceId${foodModel.foodId}" style="margin-left: 110px;"><c:out value="${foodModel.price}"/></span>
<span> €</span>
</td>
<td>
<span class="eachPrice" id="totalPriceId${foodModel.foodId}" style="margin-left: 110px;"><c:out value="${foodModel.totalPrice}"/></span>
<span> €</span>
</td>
</tr>
</table>
</div>
</c:if>
</c:forEach>
<span class="itemList" style="color: #80FF00; margin-left: -300px">Total:</span>
<span id="totalPayment" class="itemList" style="color: #80FF00; margin-left: 5px"></span>
<span class="itemList" style="color: #80FF00;"> €</span><br/>
<div class="itemList" style="margin-left:-1130px">Your Name: <input path="ownerName" name="orderName" class="myInbox" id="orderName"/></div>
<button id="viewMyOrder" class="greyButton" style="position:relative; left: -530px">Confirm My Order</button>
</form:form>
这是我的OrdersModel
:
public class OrdersModel {
private String ownerName;
private List<Order> orders;
//getters and setters
}
如我在代码中评论的那样,如何将数据复制到ordersModel
中。
更新
我的控制器看起来像这样:
@RequestMapping(value = "/myOrder", method = RequestMethod.POST)
public String viewMyOrder(Model model,
@ModelAttribute("menuModel")MenuModel menuModel) {
logger.info("You are in view my order page.");
List<FoodModel> foodModelList = new ArrayList<FoodModel>();
for (FoodModel foodModel : menuModel.getFoodList()) {
FoodModel foodModelNew = new FoodModel();
if (foodModel.getQuantity()!=0){
foodModelNew.setFoodId(foodModel.getFoodId());
foodModelNew.setFoodName(foodModel.getFoodName());
foodModelNew.setQuantity(foodModel.getQuantity());
foodModelNew.setPrice(foodModel.getPrice());
foodModelNew.setTotalPrice(foodModel.getQuantity() * foodModel.getPrice());
}
foodModelList.add(foodModelNew);
}
menuModel.setFoodList(foodModelList);
return "myOrder";
}
@RequestMapping(value = "/OrderSent", method = RequestMethod.POST)
public String orderSent(@ModelAttribute("ordersModel")OrdersModel ordersModel,
@RequestParam("orderName") String owner, Model model) {
logger.debug("Your order has been received");
model.addAttribute("owner", owner);
return "orderSent";
}
}
答案 0 :(得分:1)
为什么不在控制器中用OrdersModel.orders
填充FoodModel
,然后渲染JSP?
<form:form id="myOrderMenuId" action="${pageContext.request.contextPath}/OrderSent" method="post" commandName="ordersModel" >
<c:forEach items="#{ordersModel.order}" var="foodModel" varStatus="status">
...
控制器:
@RequestMapping(value = "/myOrder", method = RequestMethod.POST)
public String viewMyOrder(Model model, @ModelAttribute("menuModel")MenuModel menuModel) {
logger.info("You are in view my order page.");
OrdersModel ordersModel = new OrdersModel()
List<FoodModel> foodModelList = new ArrayList<FoodModel>();
for (FoodModel foodModel : menuModel.getFoodList()) {
Order foodModelNew = new Order();
if (foodModel.getQuantity()!=0){
foodModelNew.setFoodId(foodModel.getFoodId());
foodModelNew.setFoodName(foodModel.getFoodName());
foodModelNew.setQuantity(foodModel.getQuantity());
foodModelNew.setPrice(foodModel.getPrice());
foodModelNew.setTotalPrice(foodModel.getQuantity() * foodModel.getPrice());
}
ordersModel.add(foodModelNew);
}
model.addAttribute("ordersModel", ordersModel);
return "myOrder";
}