Schauen wir uns den Effekt an:
Siedlung
list.jsp
<a href = "JavaScript: jiesuan ();" id = "jiesuan"> </a>
<form id = "jiesuanform" action = "<c: url value = '/cartitemServlet' //" method = "post"> <Eingabe type = "Hidden" name = "cartitemids" id = "cartitemids"/> <input type = "Hidden" name "name" name "method" hidDentotal "/> <Input type" hidden "name" name "name" value = "loadCartItems"/> </form> Funktion jiesuan () {// 1. Holen Sie sich die IDs aller ausgewählten Einträge und legen Sie sie in das Array var Cartitemidarray = new Array (); $ (": checkBox [name = puCtBoxBtn] [checked = true]"). Jede (Funktion () {CartItemidarray.push ($ (this) .val (); // Fügen Sie den Wert des Kontrollkästchens dem Kontrollkästchen hinzu}); // 2. Fügen Sie den Wert des Array toString () hinzu und weisen Sie ihn dann den Cartitemiden des Formulars zu, das versteckt ist ("#cartitemids"). Val (Cartitemidarray.toString ()); // Sparen Sie den Gesamtwert auf das Formular $ ("#hidDentotal"). Val ($ ("#tatal"). Text ()); // 3.. Servlet
public String loadCartItems (httpServletRequest req, httpServletResponse resp) löst ServletException aus, iOException { /** 1. double Total = double.Parsedouble (req.getParameter ("Gesamt")); / * * 2. GET LIST <KartItem> über Service */ list <Kartitem> cartitemlist = cartitemService.loadCartitems (CartItemids); / * * 3.. Speichern und weiterleiten auf /cart/showitem.jsp */req.setattribute ("CartItemList", CartItemlist); req.setattribute ("total", insgesamt); req.setattribute ("cartitemids", cartitemids); return "f: /jsps/cart/showitem.jsp";}Dao
Laden Sie mehrere Cartitems
öffentliche Liste <Cartitem> loadCartItems (String Cartitemids) löst SQLEXception { /** 1. um. /** 2. erzeugen Sie die Wehre -Klausel*/ string wheresql = tOWHERESQL (Cartitemidarray.length); / * * 3.. SQL -Anweisung erzeugen */ string sql = "SELECT * Von t_cartItem c, t_book b wobei C.bid = B.bid und" + whersql; / * * 4. Führen Sie SQL und Return List <Kartitem> */ return toCartItemlist (qr.Query (SQL, neuer Maplisthandler (), CartItemidarray);};};} aus.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 = "methode" value = "createorder"/> <table align = "center" cellpadding = "0" cellpacing = "0"> <tr bgcolor = "#efeae5"> <td colspan = "5"> <span style = "font-weight: 900; <Td> Buchname </td> <td> Einheitspreis </td> <td> Menge </td> <td> subtotal </td> </tr> <c: foreach items = "$ {cartitemlist}" var = "cartitem"> <tr align = "center"> <td align = "rechts"> <a href = "< value = '/JSPS/book/desc.jsp'/> "> <img align =" top "src =" <c: url value = '/$ {cartitem.book.image_b}' // "/> </a> </td> <td align =" links "> <a href =" < value = '/JSPS/book/desc.jsp'/> "> <span> $ {cartitem.book.bname} </span> </a> </td> <td> $ {cartitem.book · 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 = "linksSubmit" href = "javaScript: $ ('#Form1'). Subjekt ();"> Bestellung senden </a> </td> </tr> </table> </form> </c: ansonsten