victory的博客

长安一片月,万户捣衣声

0%

基于JavaWeb的网上购物商城 | 我的订单

我的订单

实现功能:
当用户点击”我的订单时“展示用户提交过的所有订单信息、购买商品信息等。

header.jsp
更改”我的订单”链接的地址。

<div class="col-md-3" style="padding-top:20px">
    <ol class="list-inline">
        <c:if test="${empty user}">
            <li><a href="login.jsp">登录</a></li>
            <li><a href="register.jsp">注册</a></li>
        </c:if>
        <c:if test="${not empty user}">
            <li>${user.username}</li>
            <li><a href="cart.jsp">购物车</a></li>
            <li><a href="${pageContext.request.contextPath}/product?method=myOrder">我的订单</a></li>
        </c:if>
    </ol>
</div>

ProductServlet.java
在ProductServlet的doGet方法中添加myOrder的判断并完成myOrder方法。

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String method = request.getParameter("method");
    if("getProById".equals(method)) {
        getProById(request, response);
    }else if("findListByCate".equals(method)) {
        findListByCate(request, response);
    }else if("cart".equals(method)) {
        cart(request, response);
    }else if("delCart".equals(method)) {
        delCart(request, response);
    }else if("clearCart".equals(method)) {
        clearCart(request, response);
    }else if("showOrder".equals(method)) {
        showOrder(request, response);
    }else if("submitOrder".equals(method)) {
        submitOrder(request, response);
    }else if("myOrder".equals(method)) {
        myOrder(request, response);
    }
}

public void myOrder(HttpServletRequest request, HttpServletResponse response) {
    // TODO Auto-generated method stub
    try {
        User user = (User)request.getSession().getAttribute("user");
        String uid = user.getUid();
        
        ProductService ps = new ProductService();
        List<Order> list = ps.findOrderList(uid);
        
        //循环读出item项
        for(Order o:list) {
            List<OrderItem> arr = ps.findItemList(o.getOid());
            
            for(OrderItem oo:arr) {
                Product pro = ps.getProById(oo.getPid());
                oo.setPro(pro);
            }
            o.setList(arr);
        }
        
        //返回到订单列表页面
        request.setAttribute("list", list);
        try {
            request.getRequestDispatcher("/order_list.jsp").forward(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

ProductService.java
在ProductService中添加findOrderList、findItemList方法。

public List<Order> findOrderList(String uid) throws SQLException {
    // TODO Auto-generated method stub
    ProductDao pd = new ProductDao();
    List<Order> list = pd.findOrderList(uid);
    return list;
}

public List<OrderItem> findItemList(String oid) throws SQLException {
    // TODO Auto-generated method stub
    ProductDao pd = new ProductDao();
    List<OrderItem> list = pd.findItemList(oid);
    return list;
}

ProductDao.java
在ProductDao中添加findOrderList、findItemList方法。

public List<Order> findOrderList(String uid) throws SQLException {
    // TODO Auto-generated method stub
    QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
    
    String sql = "select * from orders where uid=? order by ordertime desc";
    
    List<Order> list = qr.query(sql, new BeanListHandler<>(Order.class), uid);
    return list;
}

public List<OrderItem> findItemList(String oid) throws SQLException {
    // TODO Auto-generated method stub
    QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
    
    String sql = "select * from orderitem where oid=?";
    
    List<OrderItem> list = qr.query(sql, new BeanListHandler<>(OrderItem.class), oid);
    return list;
}

order_list.jsp
在order_list.jsp页面解析所有订单。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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; width: 950px;">
                <strong>我的订单</strong>
                <table class="table table-bordered">
                    <tbody>
                        <c:forEach items="${list}" var="orders">
                                <tr class="success">
                                    <th colspan="2">订单编号:${orders.oid}</th>
                                    <th colspan="2" style="text-align:center">订单时间:${orders.ordertime}</th>
                                    <th colspan="1" >是否支付:
                                        <c:if test="${orders.state==1}">已支付</c:if>
                                        <c:if test="${orders.state==0}">未支付</c:if>
                                    </th>
                                </tr>
                                <tr class="warning">
                                    <th>图片</th>
                                    <th>商品</th>
                                    <th>价格</th>
                                    <th>数量</th>
                                    <th>小计</th>
                                </tr>
                                <c:forEach items="${orders.list}" var="item">
                                    <tr class="active">
                                        <td width="60" width="40%"><input type="hidden" name="id"
                                            value="22"> <img src="${pageContext.request.contextPath}/${item.pro.pimage}" width="70"
                                            height="60"></td>
                                        <td width="30%"><a target="_blank">${item.pro.pname}</a></td>
                                        <td width="20%">¥${item.pro.shop_price}</td>
                                        <td width="10%">${item.count}</td>
                                        <td width="15%"><span class="subtotal">¥${item.subtotal}</span></td>
                                    </tr>
                                </c:forEach>
                                
                        </c:forEach>
                        
                    </tbody>
                </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>