一.購(gòu)買頁(yè)面
1.ForeServlet.buyone()
在產(chǎn)品頁(yè)面點(diǎn)擊立即購(gòu)買會(huì)訪問(wèn)地址
http://127.0.0.1:8080/tmall/forebuyone?pid=844&num=3
通過(guò)訪問(wèn)地址/forebuyone導(dǎo)致ForeServlet.buyone()方法被調(diào)用
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;
二.結(jié)算頁(yè)面
1.ForeServlet.buy()
上一節(jié)訪問(wèn)forebuy?oiid=1頁(yè)面導(dǎo)致ForeServlet.buy()被調(diào)用
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
三.加入購(gòu)物車
四.查看購(gòu)物車
1.ForeServlet.cart()
訪問(wèn)地址/forecart導(dǎo)致ForeServlet.cart()方法被調(diào)用
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
五.登陸狀態(tài)過(guò)濾器
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增加對(duì)ForeAuthFilter的配置
注:必須加在ForeServletFilter之前
六.購(gòu)物車頁(yè)面操作
1.調(diào)整訂單數(shù)量
點(diǎn)擊增加減少按鈕禀挫,根據(jù)cartPage.jsp中的js代碼卧秘,會(huì)通過(guò)Ajax訪問(wèn)/forechangeOrderItem路徑
導(dǎo)致ForeServlet.changeOrderItem()方法被調(diào)用
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.刪除訂單項(xiàng)
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;
八.我的訂單頁(yè)
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.確認(rèn)收貨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.確認(rèn)收貨成功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評(píng)價(jià)產(chǎn)品頁(yè)面 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.提交評(píng)價(jià)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";