• 案例31-我的订单


    1 案例效果

    2 案例分析

    1 我的订单的分析

    2 MapListHandler分析

    3 代码实现

    1 header.jsp代码修改

    2 ProductServlet代码

    // 10 获取登录用户所有订单信息 myOrders
    public void myOrders(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        HttpSession session = request.getSession();
        
        ProductService service = new ProductService();
        
        // 首先应该判断用户是否登录了
        User user = (User) session.getAttribute("user");
           //如果没有登录则跳转到登录页面
        if(user==null){ 
            //重定向到登录页面
            response.sendRedirect(request.getContextPath()+"/login.jsp");
            //不在执行继续执行之后的代码
            return;
        }
        
        // 根据登录用户的uid获取该用户的订单列表(单表查询orders表)
        //集合中的每一个Order对象的数据是不完整的。 缺少List<OrderItem> orderItems
        // private User user;//该订单属于哪个用户
        List<Order> orderList = null;
        try {
            orderList = service.findOrder(user);
        } catch (SQLException e) {
            
            e.printStackTrace();
        }
        
        //循环遍历所有的订单,为每一个订单填充订单项集合信息。
        if(orderList!=null){
            for (Order order : orderList) {
                //根据order的oid进行查询
                //获得每一个订单的oid
                String oid = order.getOid();
                //查询该订单的所有订单项 ---mapList封装的是多个订单项和该订单项中的商品的信息。
                List<Map<String, Object>> mapList = service.findAllItemsByOid(oid);
                //将mapList转换成List<OrderItem> orderItems
                for(Map<String, Object> map:mapList){
                    try {
                        //1  从map中取出count subtotal 封装到OrderItem中
                        //item.setCount(Integer.parseInt(map.get("count").toString()));
                        OrderItem item = new OrderItem();
                        BeanUtils.populate(item, map);
                        
                        // 2 从map中取出pimage pname shop_price 封装到product中
                        Product product = new Product();
                        BeanUtils.populate(product, map);
                        
                        // 3 将product 封装到OrderItem中
                        item.setProduct(product);
                        
                        // 4  将OrderItem封装到List<OrderItem> orderItems中
                        order.getOrderItems().add(item);
                        
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        
                        e.printStackTrace();
                    }
                    
                }
                
            }
        }
        
        
        //到这里orderList封装完整了
        request.setAttribute("orderList", orderList);
    
        //转发
        request.getRequestDispatcher("/order_list.jsp").forward(request, response);
    }

    3 ProductService代码

    //获取登录用户的订单集合
    public List<Order> findOrder(User user) throws SQLException {
        ProductDao dao = new ProductDao();
        return dao.findOrder(user);
    }
    
    //查询指定的订单的所有订单项
    public List<Map<String, Object>> findAllItemsByOid(String oid) {
        ProductDao dao = new ProductDao();
        List<Map<String, Object>> mapList =null;
        try {
            mapList = dao.findAllItemsByOid(oid);
        } catch (SQLException e) {
            
            e.printStackTrace();
        }
        return mapList;
    }

    4 ProductDao代码

    //获取登录用户的所有订单列表
    public List<Order> findOrder(User user) throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from orders where uid=?";
        List<Order> orderList = qr.query(sql, new BeanListHandler<Order>(Order.class), user.getUid());
        return orderList;
    }
    
    // 查询指定的订单的所有订单项
    public List<Map<String, Object>> findAllItemsByOid(String oid) throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select i.count,i.subtotal,p.pname,p.pimage,p.shop_price"
                + " from orderitem i,product p "
                + "where i.pid=p.pid and oid=?";
        List<Map<String, Object>> mapList = qr.query(sql, new MapListHandler(), oid);
        return mapList;
    }

    5 order_list.jsp代码

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <!DOCTYPE html>
    <html>
    
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>会员登录</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
    <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
    <script src="js/bootstrap.min.js" type="text/javascript"></script>
    <!-- 引入自定义css文件 style.css -->
    <link rel="stylesheet" href="css/style.css" type="text/css" />
    
    <style>
    body {
        margin-top: 20px;
        margin: 0 auto;
    }
    
    .carousel-inner .item img {
        width: 100%;
        height: 300px;
    }
    </style>
    </head>
    
    <body>
    
    
        <!-- 引入header.jsp -->
        <jsp:include page="/header.jsp"></jsp:include>
    
        <div class="container">
            <div class="row">
                <div style="margin: 0 auto; margin-top: 10px;  950px;">
                    <strong>我的订单</strong>
                    <table class="table table-bordered">
                       <c:forEach items="${requestScope.orderList }" var="order">
                            <tbody>
                                <tr class="success">
                                    <th colspan="5">订单编号:${order.oid} &nbsp;&nbsp;&nbsp;&nbsp;${order.state==0?"未付款":"已付款" }</th>
                                </tr>
                                <tr class="warning">
                                    <th>图片</th>
                                    <th>商品</th>
                                    <th>价格</th>
                                    <th>数量</th>
                                    <th>小计</th>
                                </tr>
                                <c:forEach items="${order.orderItems }" var="orderItem">
                                    <tr class="active">
                                        <td width="60" width="40%"><input type="hidden" name="id"
                                            value="22"> <img src="${pageContext.request.contextPath }/${orderItem.product.pimage}" width="70"
                                            height="60"></td>
                                        <td width="30%"><a target="_blank">${orderItem.product.pname}</a></td>
                                        <td width="20%">¥${orderItem.product.shop_price }</td>
                                        <td width="10%">${orderItem.count }</td>
                                        <td width="15%"><span class="subtotal">¥${orderItem.subtotal }</span></td>
                                    </tr>
                                </c:forEach>
                            </tbody>
                        </c:forEach>
                    </table>
                </div>
            </div>
            <div style="text-align: center;">
                <ul class="pagination">
                    <li class="disabled"><a href="#" aria-label="Previous"><span
                            aria-hidden="true">&laquo;</span></a></li>
                    <li class="active"><a href="#">1</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">4</a></li>
                    <li><a href="#">5</a></li>
                    <li><a href="#">6</a></li>
                    <li><a href="#">7</a></li>
                    <li><a href="#">8</a></li>
                    <li><a href="#">9</a></li>
                    <li><a href="#" aria-label="Next"> <span aria-hidden="true">&raquo;</span>
                    </a></li>
                </ul>
            </div>
        </div>
    
        <!-- 引入footer.jsp -->
        <jsp:include page="/footer.jsp"></jsp:include>
        
    </body>
    
    </html>
  • 相关阅读:
    加载中动画
    跑步动画
    关键帧动画
    animate.css
    怪异盒子
    弹性项目属性
    改变元素大小
    Linux 文件系统 --磁盘I/O
    Linux 文件系统
    Sample Test Strategy
  • 原文地址:https://www.cnblogs.com/jepson6669/p/8449175.html
Copyright © 2020-2023  润新知