我对spring很新,现在正尝试在spring3中开发一个简单的webshop演示应用程序。 在应用程序中,我已经使用必要的getter和setter(articleNumber,articleName,articleDescription,articlePrice,articleCategory)定义了一个ArticleBean。 OrderlineBean包含属性数量(整数)和文章(ArticleBean) Cart具有HashMap()
类型的属性顺序线jsp的以下部分产生正确的渲染形式。例如文章数据正确显示。
<form:form method="post" action="updatecart.htm" modelAttribute="subStock">
<c:forEach var="orderline" items="${subStock.orderlines}" varStatus="status">
<div id="orderLine">
<input hidden="true" name="orderline.key" value="${orderline.key}"/>
<div class="articleName"><p>${orderline.value.article.articleName}</p></div>
<div class="articleOrderedItems">
<select name="orderline[${orderline.key}].value.quantity">
<c:forEach begin="0" end="${orderline.value.quantity}" var="items">
<option value="${items}">${items}</option>
</c:forEach>
</select>
<div class="articlePrice">
<fmt:formatNumber minIntegerDigits="1" maxFractionDigits="2" minFractionDigits="2" type="number" var="amount" value="${orderline.value.article.articlePrice/100}"/>
<p>€ ${amount}</p>
</div>
</div>
<div class="articleDescription"><p>${orderline.value.article.articleDescription}</p></div>
</div>
</c:forEach>
<input type="submit" value="Add to cart" />
</form:form>
在发布表单时,当控制器中的ModelAndView updateCart处理时,属性subStock中的订单行数为0。
@RequestMapping(value = "/updatecart", method = RequestMethod.POST)
public ModelAndView updateCart(Map map, @ModelAttribute("subStock") Cart subStock) {
System.out.println("SantosController /updatecart POST") ;
System.out.println(subStock.size()) ;
HashMap<String,OrderlineBean> orderlines = subStock.getOrderlines() ;
Iterator orders = orderlines.keySet().iterator() ;
while (orders.hasNext()) {
String articleID = (String) orders.next() ;
OrderlineBean orderline = orderlines.get(articleID) ;
System.out.println(orderline.getArticle().getArticleDescription() + " )" + orderline.getQuantity() + ")") ;
}
DbMapper db = new DbMapper() ;
// Set container URL
map.put("containerURL","content_default.jsp" ) ;
return new ModelAndView("santos_index");
}
任何人都可以帮助我朝着正确的方向前进吗?
干杯, 彼得
我简化了jsp表单,但结果仍然相同......
<form:form method="post" action="updatecart.htm" modelAttribute="subStock">
<c:forEach var="orderline" items="${subStock.orderlines}" varStatus="status">
<input name="orderline['${orderline.key}']" value="${orderline.value}"/>
</c:forEach>
<input type="submit" value="Add to cart" />
</form:form>