网上图书商城项目学习笔记-019我的订单

一、流程分析

网上图书商城项目学习笔记-019我的订单_第1张图片

二、代码

1.view层

(1)top.jsp

<a href="<c:url value='/OrderServlet?method=myOrders'/>" target="body">我的传智订单</a>&nbsp;&nbsp;|&nbsp;&nbsp;

 

(2)list.jsp

 1 <div class="divMain">
 2     <div class="divTitle">
 3         <span style="margin-left: 150px;margin-right: 280px;">商品信息</span>
 4         <span style="margin-left: 40px;margin-right: 38px;">金额</span>
 5         <span style="margin-left: 50px;margin-right: 40px;">订单状态</span>
 6         <span style="margin-left: 50px;margin-right: 50px;">操作</span>
 7     </div>
 8     <br/>
 9     <table align="center" border="0" width="100%" cellpadding="0" cellspacing="0">
10 
11 
12 <c:forEach items="${pb.beanList }" var="o">
13 
14         <tr class="tt">
15             <td width="320px">订单号:<a  href="<c:url value='/OrderServlet?method=load&oid=${o.oid }'/>">${o.oid }</a></td>
16             <td width="200px">下单时间:${o.orderTime }</td>
17             <td>&nbsp;</td>
18             <td>&nbsp;</td>
19             <td>&nbsp;</td>
20             <td>&nbsp;</td>
21         </tr>
22         <tr style="padding-top: 10px; padding-bottom: 10px;">
23             <td colspan="2">
24 
25 
26     <c:forEach items="${o.orderItems }" var="oi">
27     <a class="link2" href="<c:url value='/BookServlet?method=load&bid=${oi.book.bid }'/>">
28         <img border="0" width="70" src="<c:url value='/${oi.book.image_b }'/>"/>
29     </a>
30     </c:forEach>
31 
32 
33             </td>
34             <td width="115px">
35                 <span class="price_t">&yen;${o.total }</span>
36             </td>
37             <td width="142px">
38 <c:choose>
39     <c:when test="${o.status eq 1 }">(等待付款)</c:when>
40     <c:when test="${o.status eq 2 }">(准备发货)</c:when>
41     <c:when test="${o.status eq 3 }">(等待确认)</c:when>
42     <c:when test="${o.status eq 4 }">(交易成功)</c:when>
43     <c:when test="${o.status eq 5 }">(已取消)</c:when>
44 </c:choose>
45             </td>
46             <td>
47             <a href="<c:url value='/OrderServlet?method=load&oid=${o.oid }'/>">查看</a><br/>
48 <c:if test="${o.status eq 1 }">
49                 <a href="<c:url value='/OrderServlet?method=load&oid=${o.oid }'/>">支付</a><br/>
50                 <a href="<c:url value='/OrderServlet?method=load&btn=cancel&oid=${o.oid }'/>">取消</a><br/>                        
51 </c:if>
52 <c:if test="${o.status eq 3 }">
53                 <a href="<c:url value='/OrderServlet?method=load&btn=confirm&oid=${o.oid }'/>">确认收货</a><br/>
54 </c:if>
55             </td>
56         </tr>
57 </c:forEach>
58 
59     </table>
60     <br/>
61     <%@include file="/jsps/pager/pager.jsp" %>
62 </div>

 

3.pager.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 3 <script type="text/javascript">
 4     function _go() {
 5         var pc = $("#pageCode").val();//获取文本框中的当前页码
 6         if(!/^[1-9]\d*$/.test(pc)) {//对当前页码进行整数校验
 7             alert('请输入正确的页码!');
 8             return;
 9         }
10         if(pc > ${pb.totalPages}) {//判断当前页码是否大于最大页
11             alert('请输入正确的页码!');
12             return;
13         }
14         location = "${pb.url}&pc=" + pc;
15     }
16 </script>
17 
18 <div class="divBody">
19   <div class="divContent">
20     <%--上一页 --%>
21 
22 <c:choose>
23   <c:when test="${pb.currentPage eq 1 }"><span class="spanBtnDisabled">上一页</span></c:when>
24   <c:otherwise> <a href="${pb.url }&pb=${pb.currentPage-1}" class="aBtn bold">上一页</a></c:otherwise>
25 </c:choose>
26        
27 <%--我们需要计算页码列表的开始和结束位置,即两个变量begin和end,设定总共显示6页
28 计算它们需要通过当前页码!
29 1. 总页数不足6页--> begin=1, end=最大页
30 2. 总页数大于6页,通过公式设置begin和end,begin=当前页-2,end=当前页+3,因为假定总共显示6页
31 3. 如果begin<1,那么让begin=1,end=6
32 4. 如果end>tp, 让begin=tp-5, end=tp
33  --%>
34  <c:choose>
35    <c:when test="${pb.totalPages <= 6 }">
36      <c:set var="begin" value="1"/>
37      <c:set var="end" value="${pb.totalPages }"/>
38    </c:when>
39    <c:otherwise>
40      <c:set var="begin" value="${pb.currentPage -2 }"/>
41      <c:set var="end" value="${pb.currentPage + 3 }"/>
42      <c:if test="${begin < 1 }">
43        <c:set var="begin" value="1"/>
44        <c:set var="end" value="6"/>
45      </c:if>
46      <c:if test="${end > pb.totalPages }">
47        <c:set var="begin" value="${pb.totalPages - 5 }"/>
48        <c:set var="end" value="${pb.totalPages }"/>
49      </c:if>
50    </c:otherwise>
51  </c:choose>
52     
53     <%-- 显示页码列表 --%>
54     <c:forEach begin="${begin }" end="${end }" var="i">
55       <c:choose>
56         <c:when test="${i eq pb.currentPage }"><span class="spanBtnSelect">${i }</span></c:when>
57         <c:otherwise><a href="${pb.url }&pc=${i}" class="aBtn">${i}</a></c:otherwise>
58       </c:choose>
59     </c:forEach>
60     
61     <%-- 显示点点点 --%>
62     <c:if test="${end < pb.totalPages }"><span class="spanApostrophe">...</span> </c:if>
63     
64      <%--下一页 --%>
65      <c:choose>
66          <c:when test="${pb.currentPage eq pb.totalPages }"><span class="spanBtnDisabled">下一页</span></c:when>
67          <c:otherwise><a href="${pb.url }&pc=${pb.currentPage+1}" class="aBtn bold">下一页</a> </c:otherwise>
68      </c:choose>
69         
70     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
71     
72     <%-- 共N页 到M页 --%>
73     <span>共${pb.totalPages}页</span>
74     <span>到</span>
75     <input type="text" class="inputPageCode" id="pageCode" value="${pb.currentPage }"/>
76     <span>页</span>
77     <a href="javascript:_go();" class="aSubmit">确定</a>
78   </div>
79 </div>

 

2.servlet层

(1)OrderServlet.java 

 1     /**
 2      * 我的订单
 3      * @param req
 4      * @param resp
 5      * @return
 6      * @throws ServletException
 7      * @throws IOException
 8      */
 9     public String myOrders(HttpServletRequest req, HttpServletResponse resp)
10             throws ServletException, IOException {
11         int pc = getPc(req);
12         String url = getUrl(req);
13         User user = (User) req.getSession().getAttribute("sessionUser");
14         PageBean<Order> pb = service.myOrders(user.getUid(), pc);
15         pb.setUrl(url);
16         req.setAttribute("pb", pb);
17         return "f:/jsps/order/list.jsp";
18     }
19 
20     /**
21      * 获取当前请求的url
22      * @param req
23      * @return
24      */
25     private String getUrl(HttpServletRequest req) {
26         String url = req.getRequestURI() + "?" + req.getQueryString();
27         /*
28          * 如果url中存在pc参数,截取掉,如果不存在那就不用截取。
29          */
30         int index = url.lastIndexOf("&pc=");
31         if(index != -1) {
32             url = url.substring(0, index);
33         }
34         return url;
35     }
36 
37     /**
38      * 获取当前页
39      * @param req
40      * @return
41      */
42     private int getPc(HttpServletRequest req) {
43         int pc = 1;
44         String param = req.getParameter("pc");
45         if(param != null && !param.trim().isEmpty()) {
46             try {
47                 pc = Integer.parseInt(param);
48             } catch (Exception e) {
49                 e.printStackTrace();
50             }
51         }
52         return pc;
53     }

 

3.service层

(1)OrderService.java 

 1     /**
 2      * 我的订单
 3      * @param uid
 4      * @param pc
 5      * @return
 6      */
 7     public PageBean<Order> myOrders(String uid, int pc) {
 8         try {
 9             JdbcUtils.beginTransaction();
10             PageBean<Order> pb = dao.findByUser(uid, pc);
11             JdbcUtils.commitTransaction();
12             return pb;
13         } catch (SQLException e) {
14             try {
15                 JdbcUtils.rollbackTransaction();
16             } catch (SQLException e1) {
17                 e1.printStackTrace();
18             }
19             throw new RuntimeException(e);
20         }
21     }

 

4.dao层

(1)OrderDao.java

  1     /**
  2      * 按用户id查找
  3      * @param uid
  4      * @return
  5      * @throws SQLException 
  6      */
  7     public PageBean<Order> findByUser(String uid, int pc) throws SQLException {
  8         List<Expression> exprList = new ArrayList<Expression>();
  9         exprList.add(new Expression("uid", "=", uid));
 10         return findByCriteria(exprList, pc);
 11     }
 12 
 13     /**
 14      * 按条件查找
 15      * @param exprList
 16      * @return
 17      * @throws SQLException 
 18      */
 19     private PageBean<Order> findByCriteria(List<Expression> exprList, int pc) throws SQLException {
 20         /*
 21          * 1. 得到pageSize
 22          * 2. 得到totalRecords
 23          * 3. 得到beanList
 24          * 4. 创建PageBean,返回
 25          */
 26         // 1. 得到pageSize
 27         int ps = PageConfig.ORDER_PAGE_SIZE;
 28         
 29         // 2. 通过exprList来生成where子句
 30         StringBuilder sb = new StringBuilder(" where 1=1");
 31         List<Object> params = new ArrayList<Object>();
 32         for(Expression expr : exprList) {
 33             sb.append(" and ").append(expr.getName()).append(" ")
 34                 .append(expr.getOperator()).append(" ");
 35             if(!expr.getOperator().equalsIgnoreCase("is null")) {
 36                 sb.append("?");
 37                 params.add(expr.getValue());
 38             }
 39         }
 40         
 41         // 3. 总记录数 
 42         String sql = "select count(*) from t_order" + sb;
 43         Number number = (Number)qr.query(sql, new ScalarHandler(), params.toArray());
 44         int tr = number.intValue();
 45         
 46         // 4. 得到beanList,即当前页记录
 47         sql = "select * from t_order" + sb + " order by ordertime desc limit ?,?";
 48         params.add((pc - 1) * ps);//当前页首行记录的下标
 49         params.add(ps);
 50         
 51         List<Order> beanList = qr.query(sql, new BeanListHandler<Order>(Order.class), params.toArray());
 52         // 虽然已经获取所有的订单,但每个订单中并没有订单条目。
 53         // 遍历每个订单,为其加载它的所有订单条目
 54         for(Order order : beanList) {
 55             loadOrderItem(order);
 56         }
 57         
 58         // 5. 创建PageBean,设置参数
 59         PageBean<Order> pb = new PageBean<Order>();
 60         pb.setBeanList(beanList);
 61         pb.setCurrentPage(pc);
 62         pb.setPageSize(ps);
 63         pb.setTotalRecords(tr);
 64         return pb;
 65     }
 66 
 67     /*
 68      * 为指定的order载它的所有OrderItem
 69      */
 70     private void loadOrderItem(Order order) throws SQLException {
 71         String sql = "select * from t_orderItem where oid=?";//t_orderItem已经包含book表的一些字段
 72         List<Map<String,Object>> mapList = qr.query(sql, new MapListHandler(), order.getOid());
 73         List<OrderItem> orderItems = toOrderItemList(mapList);
 74         order.setOrderItems(orderItems);
 75     }
 76 
 77     /**
 78      * 把多个Map转换成多个OrderItem
 79      * @param mapList
 80      * @return
 81      */
 82     private List<OrderItem> toOrderItemList(List<Map<String, Object>> mapList) {
 83         List<OrderItem> items = new ArrayList<OrderItem>();
 84         OrderItem item = null;
 85         for(Map<String,Object> map : mapList) {
 86             item = toOrderItem(map);
 87             items.add(item);
 88         }
 89         return items;
 90     }
 91 
 92     /*
 93      * 把一个Map转换成一个OrderItem
 94      */
 95     private OrderItem toOrderItem(Map map) {
 96         OrderItem item = CommonUtils.toBean(map, OrderItem.class);
 97         Book book = CommonUtils.toBean(map, Book.class);
 98         item.setBook(book);
 99         return item;
100     }

 

你可能感兴趣的:(网上图书商城项目学习笔记-019我的订单)