I l @ ve RuBoard |
Buying a single item is relatively straightforward because you can handle it directly from the URL. But the client wants the customer to be able to purchase several items at the same time. Listing 10.7 shows a section of Category.jsp rewritten to let the customer buy a quantity of multiple books. It replaces the current product display loop. Listing 10.7 Category.jsp Revisited<FORM METHOD="POST" ACTION="buyit.jsp" TARGET="tempwindow"> <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber4" height="253"> <tr> <th width="38%" height="23" align="left">Title</th> <th width="23%" height="23" align="left">Author</th> <th width="11%" height="23" align="left">Price</th> <th width="28%" height="23" align="center">Quantity</td> </tr> <% Iterator iter = cat.getProducts().iterator(); while (iter.hasNext()) { Product prod = (Product) iter.next(); %> <tr> <td width="38%" height="38" bgcolor="#00FFFF"> <span style="font-size: 10.0pt; font-family: Times New Roman"> <A HREF="Product.jsp?ISBN=<%= prod.getISBN() %>"> <%= prod.getTitle() %></A> </span></td> <td width="23%" height="38" bgcolor="#00FFFF"> <span style="font-size: 10.0pt; font-family: Times New Roman"> <%= prod.getAuthorString() %> </span></td> <td width="11%" height="38" bgcolor="#00FFFF"> <span style="font-size: 10.0pt; font-family: Times New Roman"> <%= prod.getPriceString() %> </span></td> <td width="28%" height="38" bgcolor="#00FFFF"> <p align="center"><font size="2"> <INPUT NAME="ISBN<%= prod.getISBN() %>" TYPE="TEXT" SIZE=2> <INPUT TYPE="SUBMIT" VALUE="Buy"> </font></td> </tr> </table> </form> The entire list of books has been wrapped in a table, a new heading called Quantity has been added to the table, and form fields have been placed beside each item to record the quantity. The Buy link has been changed to form Submit buttons (see Figure 10.2). Figure 10.2. The multiple-buy category page.
You might have noticed that the letters ISBN were added to the ISBN code for each book and that it was used as the form field name for each book. When you look at the revised buyit.jsp (see Listing 10.8), it becomes clear why. Listing 10.8 A Second Go at buyit.jsp<%@ page import="com.bfg.product.Product" %> <%@ page import="com.bfg.cart.Cart" %> <%@ page import="java.util.Enumeration" %> <%@ page import="java.text.NumberFormat" %> <jsp:useBean id="cart" class="com.bfg.cart.Cart" scope="session"/> <% Enumeration names = request.getParameterNames(); NumberFormat nf = NumberFormat.getInstance(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); if (name.startsWith("ISBN")) { String ISBN = name.substring(4); if ((request.getParameter(name) != null) && (request.getParameter(name).length() > 0)) { try { int quantity = nf.parse(request.getParameter(name)).intValue(); if (quantity > 0) { out.print("Looking for " + ISBN + "<BR>"); Product prod = Product.findProduct(ISBN); if (prod != null) { cart.addItem(prod, quantity); } } } catch (NumberFormatException e) { } } } } %> <SCRIPT> if (window.opener && !window.opener.closed) window.opener.location.reload(); window.close(); </SCRIPT> You need to prepend ISBN to the field name because a number of other parameters might get handed to buyit , including the Submit buttons. To make it easier to pick the actual quantity fields out of the crowd , you put a distinct tag in front to make them stand out. First, get a list of all the parameters. Iterate over them, looking for ones starting with ISBN. If the value is not blank, try parsing the value (if the customer types "garbage" into a field, it will throw a number format exception and skip over that field). Then, if the quantity is more than 0 (you wouldn't want someone entering " “80" and ending up with a credit of several thousand dollars!), add the quantity to the cart.
|
I l @ ve RuBoard |