概念
上一文中,我们实现了注册,登录,提交订单以及修改个人信息等功能。本文在登录的状态下,实现订单列表以及退出登录功能等。
我的订单
在head.jsp头部页面中,当用户处于登录状态,则会显示“我的订单”选项
点击“我的订单”后,触发超链接向服务器发送请求,因此需要在servlet包中创建OrderListServlet类,用于接收浏览器请求,查询当前用户的所有订单信息,并返回给浏览器显示
package servlet;import model.Order;
import model.User;
import service.OrderService;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;@WebServlet(name = "order_list", urlPatterns = "/order_list")
public class OrderListServlet extends HttpServlet {private OrderService oService = new OrderService();protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {User u = (User) request.getSession().getAttribute("user");if(u==null){response.sendRedirect("/index");return;}List<Order> list = oService.selectAll(u.getId());request.setAttribute("orderList", list);request.getRequestDispatcher("/order_list.jsp").forward(request, response);}
}
以上代码中,接收到浏览器的请求后,先判断session中是否存在user对象,也就是判断是否用户处于登录状态,如果未登录,则回到首页,处于登录状态,则将用户编号发送给业务逻辑层执行操作。
因此需要在OrderService类中定义对应方法
public List<Order> selectAll(int userid){List<Order> list=null;try {list = oDao.selectAll(userid);for(Order o :list) {List<OrderItem> l = oDao.selectAllItem(o.getId());o.setItemList(l);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return list;
}
以上代码中,通过用户编号传递给数据访问层dao层执行查询该用户的所有订单信息,但可能一个订单中包含多种商品的购买信息,因此还需要根据每一个订单编号获取当前这次订单中购买的所有商品信息。
因此需要在OrderDao类中定义对应的方法
public List<Order> selectAll(int userid) throws SQLException {QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());String sql = "select * from `order` where user_id=? order by datetime desc";return r.query(sql, new BeanListHandler<Order>(Order.class),userid);
}
public List<OrderItem> selectAllItem(int orderid) throws SQLException{QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());String sql = "select i.id,i.price,i.amount,g.name from orderitem i,goods g where order_id=? and i.goods_id=g.id";return r.query(sql, new BeanListHandler<OrderItem>(OrderItem.class),orderid);
}
最后将查询出来的订单信息发送给order_list.jsp页面进行展示
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head><title>我的订单</title><meta name="viewport" content="width=device-width, initial-scale=1"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><link type="text/css" rel="stylesheet" href="css/bootstrap.css"><link type="text/css" rel="stylesheet" href="css/style.css"><script type="text/javascript" src="js/jquery.min.js"></script><script type="text/javascript" src="js/bootstrap.min.js"></script><script type="text/javascript" src="layer/layer.js"></script><script type="text/javascript" src="js/cart.js"></script>
</head>
<body><jsp:include page="header.jsp"><jsp:param name="flag" value="5"></jsp:param></jsp:include><div class="cart-items"><div class="container"><h2>我的订单</h2><table class="table table-bordered table-hover"><tr><th width="10%">ID</th><th width="10%">总价</th><th width="20%">商品详情</th><th width="30%">收货信息</th><th width="10%">订单状态</th><th width="10%">支付方式</th><th width="10%">下单时间</th></tr><c:forEach items="${orderList }" var="order"><tr><td><p>${order.id }</p></td><td><p>${order.total }</p></td><td><c:forEach items="${order.itemList }" var="item"><p>${item.goodsName }(${item.price }) x ${item.amount }</p></c:forEach></td><td><p>${order.name }</p><p>${order.phone }</p><p>${order.address }</p></td><td><p><c:if test="${order.status==2 }"><span style="color:red;">已付款</span></c:if><c:if test="${order.status==3 }"><span style="color:green;">已发货</span></c:if><c:if test="${order.status==4 }"><span style="color:black;">已完成</span></c:if></p></td><td><p><c:if test="${order.paytype==1 }">微信</c:if><c:if test="${order.paytype==2 }">支付宝</c:if><c:if test="${order.paytype==3 }">货到付款</c:if></p></td><td><p>${order.datetime }</p></td></tr></c:forEach></table></div></div><jsp:include page="footer.jsp"></jsp:include>
</body>
</html>
以上效果图中,当用户支付完成后,查看订单情况时,处于已付款状态,需要管理员【商家】进入后台管理系统进行发货后,用户从订单中查看到已完成状态。
退出功能
登录之后,在head.jsp头部页面中会出现退出选项,当点击退出后,会想服务器发送请求
因此在servlet包中创建UserLogoutServlet类,用于处理退出登录的操作
package servlet;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;@WebServlet(name = "user_logout",urlPatterns = "/user_logout")
public class UserLogoutServlet extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.getSession().removeAttribute("user");response.sendRedirect("/index");}
}
以上代码中,接收到浏览器的退出登录请求,需要从session中删除user对象,也就是清除用户的登录信息,然后回头首页。