Let's take a look at the effect:
Settlement
list.jsp
<a href="javascript:jiesuan();" id="jiesuan"></a>
<form id="jieSuanForm" action="<c:url value='/CartItemServlet'//" method="post"> <input type="hidden" name="cartItemIds" id="cartItemIds"/> <input type="hidden" name="total" id="hiddenTotal"/> <input type="hidden" name="method" value="loadCartItems"/></form>function jiesuan() { // 1. Get the ids of all selected entries and put them in the array var cartItemIdArray = new Array(); $(":checkbox[name=checkboxBtn][checked=true]").each(function() { cartItemIdArray.push($(this).val());// Add the value of the check box to the array}); // 2. Add the value of the array toString(), and then assign it to the cartItemIds of the form, which is hidden $("#cartItemIds").val(cartItemIdArray.toString()); // Save the total value to the form $("#hiddenTotal").val($("#tatal").text()); // 3. Submit this form $("#jieSuanForm").submit();} servlet
public String loadCartItems(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { /* * 1. Get cartItemIds parameter*/ String cartItemIds = req.getParameter("cartItemIds"); double total = Double.parseDouble(req.getParameter("total")); /* * 2. Get List<CartItem> through service */ List<CartItem> cartItemList = cartItemService.loadCartItems(cartItemIds); /* * 3. Save and forward to /cart/showitem.jsp */ req.setAttribute("cartItemList", cartItemList); req.setAttribute("total", total); req.setAttribute("cartItemIds", cartItemIds); return "f:/jsps/cart/showitem.jsp";}Dao
Load multiple CartItems
public List<CartItem> loadCartItems(String cartItemIds) throws SQLException { /* * 1. Convert cartItemIds into an array*/ Object[] cartItemIdArray = cartItemIds.split(","); /* * 2. Generate wehre clause*/ String whereSql = toWhereSql(cartItemIdArray.length); /* * 3. Generate sql statement*/ String sql = "select * from t_cartitem c, t_book b where c.bid=b.bid and " + whereSql; /* * 4. Execute sql and return List<CartItem> */ return toCartItemList(qr.query(sql, new MapListHandler(), cartItemIdArray));}showitem.jsp
<c:choose> <c:when test="${empty cartItemList }">hehe~</c:when> <c:otherwise> <form id="form1" action="<c:url value='/OrderServlet'//>" method="post"> <input type="hidden" name="cartItemIds" value="${cartItemIds }"/> <input type="hidden" name="method" value="createOrder"/> <table align="center" cellpadding="0" cellpacing="0"> <tr bgcolor="#efeae5"> <td colspan="5"><span style="font-weight: 900;">Generate order</span></td> </tr> <ttr align="center"> <td> </td> <td>Book name</td> <td>Unit price</td> <td>Quantity</td> <td>Subtotal</td> </tr> <c:forEach items="${cartItemList }" var="cartItem"> <tr align="center"> <td align="right"> <a href="<c:url value='/jsps/book/desc.jsp'/>"><img align="top" src="<c:url value='/${cartItem.book.image_b }'//"/></a> </td> <td align="left"> <a href="<c:url value='/jsps/book/desc.jsp'/>"><span>${cartItem.book.bname }</span></a> </td> <td>${cartItem.book.currPrice }</td> <td>${cartItem.quantity }</td> <td> <span>${cartItem.subtotal }</span></span> </td> </td> </tr> </c:forEach> <tr> <td colspan="6" align="right"> <span>Total: </span><span>¥<span id="total">${total }</span></span> </td> </tr> <tr> <td colspan="5" bgcolor="#efeae5"><span style="font-weight: 900">Shipping address</span></td> </tr> <tr> <td colspan="6"> <input id="addr" type="text" name="address" value="Zhang Sanye Zhang, 1st Floor, Jinyanlong Office Building, Xisanqi, Changping District, Beijing"/> </td> </tr> <tr> <td style="border-top-width: 4px;" colspan="5" align="right"> <a id="linkSubmit" href="javascript:$('#form1').submit();">Submit order</a> </td> </tr> </table> </form> </c:otherwise></c:choose>