网上图书商城项目学习笔记-017结算,跳转结算页面

一、流程分析

网上图书商城项目学习笔记-017结算,跳转结算页面_第1张图片

二、代码

1.view层

(1)list.jsp

    <tr>
        <td colspan="7" align="right">
            <a href="javascript:jiesuan();" id="jiesuan" class="jiesuan"></a>
        </td>
    </tr>
</table>
    <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. 获取所有被选择的条目的id,放到数组中
     var ids = new Array();
     $("input[type=checkbox][name=checkboxBtn]:checked").each(function(index, domEl) {
         ids.push($(domEl).val());
     });
     // 2. 把数组的值toString(),然后赋给表单的cartItemIds这个hidden
     $("#cartItemIds").val(ids);
    // 把总计的值,也保存到表单中
    $("#hiddenTotal").val($("#total").text());
    // 3. 提交这个表单
    $("#jieSuanForm").submit();
 }

 

(2)showitem.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 3 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
 4 
 5 
 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 7 <html>
 8   <head>
 9     <title>showitem.jsp</title>
10     
11     <meta http-equiv="pragma" content="no-cache">
12     <meta http-equiv="cache-control" content="no-cache">
13     <meta http-equiv="expires" content="0">    
14     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
15     <meta http-equiv="description" content="This is my page">
16     <!--
17     <link rel="stylesheet" type="text/css" href="styles.css">
18     -->
19     <link rel="stylesheet" type="text/css" href="<c:url value='/jsps/css/cart/showitem.css'/>">
20     <script src="<c:url value='/jquery/jquery-1.5.1.js'/>"></script>
21     <script src="<c:url value='/js/round.js'/>"></script>
22 <style type="text/css">
23 #addr{width: 500px; height: 32px;border: 1px solid #7f9db9; padding-left: 10px; line-height: 32px;}
24 </style>
25 
26 <script type="text/javascript">
27     //计算合计
28     $(function() {
29         var total = 0;
30         $(".subtotal").each(function() {
31             total += Number($(this).text());
32         });
33         //$("#total").text(round(total, 2));
34     });
35 </script>
36   </head>
37   
38   <body>
39 <form id="form1" action="<c:url value='/jsps/order/ordersucc.jsp'/>" method="post">
40     <input type="hidden" name="cartItemIds" value=""/>
41     <input type="hidden" name="method" value=""/>
42 <table width="95%" align="center" cellpadding="0" cellspacing="0">
43     <tr bgcolor="#efeae5">
44         <td width="400px" colspan="5"><span style="font-weight: 900;">生成订单</span></td>
45     </tr>
46     <tr align="center">
47         <td width="10%">&nbsp;</td>
48         <td width="50%">图书名称</td>
49         <td>单价</td>
50         <td>数量</td>
51         <td>小计</td>
52     </tr>
53 
54 
55 <c:forEach items="${items }" var="item">
56     <tr align="center">
57         <td align="right">
58             <a class="linkImage" href="<c:url value='/jsps/book/desc.jsp'/>"><img border="0" width="54" align="top" src="<c:url value='${item.book.image_b }'/>"/></a>
59         </td>
60         <td align="left">
61             <a href="<c:url value='/jsps/book/desc.jsp'/>"><span>${item.book.bname }</span></a>
62         </td>
63         <td>&yen;${item.book.currPrice }</td>
64         <td>${item.quantity }</td>
65         <td>
66             <span class="price_n">&yen;<span class="subtotal">${item.subtotal }</span></span>
67         </td>
68     </tr>
69 </c:forEach>
70     
71     <tr>
72         <td colspan="6" align="right">
73             <span>总计:</span><span class="price_t">&yen;<span id="total">${total }</span></span>
74         </td>
75     </tr>
76     <tr>
77         <td colspan="5" bgcolor="#efeae5"><span style="font-weight: 900">收货地址</span></td>
78     </tr>
79     <tr>
80         <td colspan="6">
81             <input id="addr" type="text" name="address" value="北京市 昌平区 西三旗 金燕龙办公楼1层 传智播客 张三爷"/>
82         </td>
83     </tr>
84     <tr>
85         <td style="border-top-width: 4px;" colspan="5" align="right">
86             <a id="linkSubmit" href="javascript:$('#form1').submit();">提交订单</a>
87         </td>
88     </tr>
89 </table>
90 </form>
91   </body>
92 </html>

 

 

2.servlet层

(1)CartItemServlet.java

 

 1     /**
 2      * 加载多个CartItem
 3      * @param req
 4      * @param resp
 5      * @return
 6      * @throws ServletException
 7      * @throws IOException
 8      */
 9     public String loadCartItems(HttpServletRequest req, HttpServletResponse resp)
10             throws ServletException, IOException {
11         // 1. 获取cartItemIds参数
12         String ids = req.getParameter("cartItemIds");
13         double total = Double.parseDouble(req.getParameter("total"));
14         // 2. 通过service得到List<CartItem>
15         List<CartItem> items = service.loadCartItems(ids);
16         // 3. 保存,然后转发到/cart/showitem.jsp
17         req.setAttribute("items", items);
18         req.setAttribute("total", total);
19         req.setAttribute("cartItemIds", ids);
20         return "f:/jsps/cart/showitem.jsp";
21         
22     }

 

3.service层

(1)CartitemService.java

 

 1     /**
 2      * 加载多个CartItem
 3      * @param ids
 4      * @return
 5      */
 6     public List<CartItem> loadCartItems(String ids) {
 7         try {
 8             return dao.loadCartItems(ids);
 9         } catch (SQLException e) {
10             throw new RuntimeException(e);
11         }
12     }

 

4.dao层

(1)CartItemDao.java

 1     /**
 2      * 加载多个CartItem
 3      * @param ids
 4      * @return
 5      * @throws SQLException
 6      */
 7     public List<CartItem> loadCartItems(String ids) throws SQLException {
 8         String [] idsArray = ids.split(",");
 9         String sql = "select * from t_cartItem c, t_book b where c.bid=b.bid and cartItemid in " + toWhereSql(idsArray.length);
10         List<Map<String,Object>> mapList = qr.query(sql, new MapListHandler(), idsArray);
11         return toCartItemList(mapList);
12     }

 

你可能感兴趣的:(网上图书商城项目学习笔记-017结算,跳转结算页面)