一.购买页面
1.ForeServlet.buyone()
在产品页面点击立即购买会访问地址
http://127.0.0.1:8080/tmall/forebuyone?pid=844&num=3
通过访问地址/forebuyone导致ForeServlet.buyone()方法被调用
int pid=Interger.parseInt(request.getParameter("pid"));
int num = Interger.parseInt(request.getParameter("num"));
Product p = productDAO.get(pid);
User user = request.getSession.getAttribute("user");
boolean found = false;
List<OrderItem> ois = orderItemDAO.listByUser(user.getId());
for(orderItem oi : ois){
if(oi.getProduct().getId() = p.getId()){
oi.setNumber(oi.getNumber+num);
orderItemDAO.update(oi);
found = true;
oiid = oi.getId();
break;
}
}
if(!found){
OrderItem oi = new OrderItem();
oi.setUser(user);
oi.setNumber(num);
oi.setProduct(p);
orderItemDAO.add(oi);
oiid = oi.getId();
}
return "@forebuy?oiid="+oiid;
二.结算页面
1.ForeServlet.buy()
上一节访问forebuy?oiid=1页面导致ForeServlet.buy()被调用
String[] oiids = request.getParameterValues("oiid");
List<OrderItem> ois = new ArrayList<>();
float total = 0;
for(String strid : oiids){
int oiid = Interger.parseInt(strid);
OrderItem oi = orderItemDAO.get(oiid);
total + = oi.getProduct().getPromotePrice()*oi.getNumber();
ois.add(oi);
}
reuqest.getSession().setAttribute("ois",ois);
request.setAttribute("total",total);
return "buy.jsp";
2.buy.jsp
header.jsp
top.jsp
buyPage.jsp
footer.jsp
三.加入购物车
四.查看购物车
1.ForeServlet.cart()
访问地址/forecart导致ForeServlet.cart()方法被调用
User user = request.getSession.getAttribute("user");
List<OrderItem> ois = OrderItemDAO.listByUser(user.getId());
request.setAttribute("ois",ois);
return "cart.jsp";
2.cart.jsp
header.jsp
top.jsp
simpleSearch.jsp
cartPage.jsp
footer.jsp
五.登陆状态过滤器
1.ForeAuthFilter
String contextPath = request.getServletContext().getContextPath();
String[] noNeedAuthPage = new String[]{
"homepage",
"checkLogin",
"register",
"loginAjax",
"login",
"product",
"category",
"search"
};
String uri = request.getRequestURI();
uri = StringUtils.remove(uri,contextPath);
if(uri.startsWith("/fore")&&!uri.startsWith("/foreServlet")){
String method = StringUtils.substringAfterLast(uri,"/fore" );
if(!Arrays.asList(noNeedAuthPage).contains(method)){
User user =(User) request.getSession().getAttribute("user");
if(null==user){
response.sendRedirect("login.jsp");
return;
}
}
}
2.web.xml
在web.xml增加对ForeAuthFilter的配置
注:必须加在ForeServletFilter之前
六.购物车页面操作
1.调整订单数量
点击增加减少按钮,根据cartPage.jsp中的js代码,会通过Ajax访问/forechangeOrderItem路径
导致ForeServlet.changeOrderItem()方法被调用
User user = request.getSession().getAttribute("user");
if(null==user)
return "%fail";
int pid = Interger.parseInt(request.getParameter("pid"));
int number = Interger.parseInt(request.getParameter("number"));
List<OrderItem> ois = orderItemDAO.listByUser(user.getId());
for(orderItem oi : ois){
if(oi.getProduct().getId() == pid){
oi.setNumber(number);
orderItemDAO.update(oi);
break;
}
}
return "%success";
2.删除订单项
ForeServlet.deleteOrderItem()
User user = request.getSession().getAttribute("user");
if(null==user)
return "%fail";
int oiid = Interger.parseInt(reuqest.getParameter("oiid"));
orderItemDAO.delete(oiid);
return "%success";
七.生成订单
1. ForeServlet.createOrder
User user = request.getSession().getAttribute("user");
List<OrderItem> ois = (List<OrderItem>)request.getSession().getAttribute("ois");
if(ois.isEmpty())
return "@login.jsp";
String address = request.getParameter("adress");
String post = request.getParameter("post");
String receiver = request.getParameter("receiver");
String mobile = request.getParameter("mobile");
String userMessage = request.getParameter("userMessage");
Order order = new Order();
String orderCode = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+RandomUtils.nextInt(10000);
order.setOrderCode(orderCode);
order.setAddress(address);
order.setPost(post);
order.setReceiver(receiver);
order.setMobile(mobile);
order.setUserMessage(userMessage);
order.setCreateDate(new Date());
order.setUser(user);
order.setStatus(OrderDAO.waitPay);
orderDAO.add(order);
float total = 0;
for(OrderItem oi : ois){
oi.setOrder(order);
orderItemDAO.update(oi);
total+=oi.getProduct().getPromotePrice()*oi.getNumber();
}
return "@forealipay?oid="+order.getId() +"&total="+total;
八.我的订单页
1.ForeServlet.bought()
User user = (User) request.getSession().getAttribute("user");
List<Ordet> os = orderDAO.list(user.getId(),OrderDAO.delete);
orderItemDAO.fill(os);
request.setAttribute("os",os);
return "bought,jsp";
2.bought.jsp
header.jsp
top.jsp
simpleSearch.jsp
boughtPage.jsp
footer.jsp
3.确认收货ForeServlet.confirmPay()
int oid = Interger.parseInt(request.getParameter("oid"));
Order o = orderDAO.get(oid);
orderItemDAO.fill(o);
request.setAttribute("o",o);
return "confirmPay.jsp";
4.confirmPay.jsp
header.jsp
top.jsp
simpleSearch.jsp
confirmPayPage.jsp
footer.jsp
5.确认收货成功ForeServlet.orderConfirmed()
int oid = Interger.parseInt(request.getParameter("oid"));
Order o = orderDAO.get(oid);
o.setStatus(orderDAO.waitReview);
o.setConfirmDate(new Date());
orderDAO.update(o);
return "orderConfirmed.jsp";
6.orderConfirmed.jsp
header.jsp
top.jsp
simpleSearch.jsp
orderConfirmedPage.jsp
footer.jsp
7.删除ForeServlet.deleteOrder()
int oid = Integer.parseInt(request.getParameter("oid"));
Order o = orderDAO.get(oid);
o.setStatus(OrderDAO.delete);
orderDAO.update(o);
return "%success";
8评价产品页面 ForeServlet.review()
int oid = Interger.parseInt(request.getParameter("oid"));
Order o = orderDAO.get(oid);
orderItemDAO.fill(o);
Product p = o.getOrderItems().get(0).getProduct();
List<Review> reviews = reviewDAO.list(p.getId());
productDAO.setSaleAndReviewNumber(p);
request.setAttribute("p", p);
request.setAttribute("o", o);
request.setAttribute("reviews", reviews);
return "review.jsp";
9.提交评价ForeServlet.doreview()
int oid = Integer.parseInt(request.getParameter("oid"));
Order o = orderDAO.get(oid);
o.setStatus(OrderDAO.delete);
orderDAO.update(o);
int pid = Interger.parseInt(request.getParameter("pid"));
Product p = productDAO.get(pid);
String content = request.getParameter("content");
content = HtmlUtils.htmlEscape(content);
User user = (User) request.getSession().getAttribute("user");
Review review = new Review();
review.setContent(content);
review.setProduct(p);
review.setCreateDate(new Date());
review.setUser(user);
reviewDAO.add(review);
return "@forereview?oid="+oid+"&showonly=true";