日期:2014-05-18 浏览次数:20827 次
... <% Cart cart = (Cart) session.getAttribute("cart"); if (session.getAttribute("cart") == null) { cart = new Cart(); } %> ... <form action="cartAction.jsp?action=update" method="post"> <table border="1" width="65%" align="center"> <tr> <td> 编号</td> <td> 书名</td> <td> 作者</td> <td> 数量</td> <td> 单价</td> <td> 总价</td> <td> <a href="cartAction.jsp?action=clear">清空购物车</a> </td> </tr> <% for (Iterator<CartItem> iterator = cart.getItems().iterator(); iterator.hasNext(); ) { CartItem item = (CartItem) iterator.next(); %> <tr> <td> <a href="bookAction.jsp?action=bookdetail&bookId=<%=item.getItem().getBookId()%>"><%=item.getItem().getBookId()%></a> </td> <td> <%=item.getItem().getTitle()%></td> <td> <%=item.getItem().getName()%></td> <!-- 这里文本框名字按bookId命名 --> <td> <input name="<%=item.getItem().getBookId()%>" type="text" value="<%=item.getQuantity()%>" size="4"></td> <td> ¥<%=item.getItem().getPrice()%></td> <td> ¥<%=item.getItemPrice()%></td> <td> <a href="cartAction.jsp?action=remove&bookId=<%=item.getItem().getBookId()%>">删除</a> </td> </tr> <% } %> <tr> <td colspan="6" align="right"> 总计:¥<%=cart.getAmount()%> <br> <input type="submit" value="更新"></td> <td> </td> </tr> <tr> <td colspan="7" align="center"><a href="userAction.jsp?action=pay">付款</a></td> </tr> </table> </form> ...
<% ... String action = request.getParameter("action"); String bookId = request.getParameter("bookId"); Cart cart = (Cart)session.getAttribute("cart"); ... if ("update".equals(action)) { for(Iterator<CartItem> iterator = cart.getItems().iterator(); iterator.hasNext(); ) { CartItem item = (CartItem)iterator.next(); String key = item.getItem().getBookId(); int value = Integer.parseInt(request.getParameter(key)); if(value > 0) { cart.update(key, value); }else{ request.setAttribute("info", "输入数量至少为1"); } } session.setAttribute("cart", cart); pageContext.forward("showcart.jsp"); } ... %>