[TOC]
## 步骤 1 : 先运行,看到效果,再学习
先将完整的 tmall_ssm 项目(向老师要相关资料),配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
## 步骤 2 : 模仿和排错
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较**正确答案** ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,**学习有效果,排错有效率**,可以较为明显地提升学习速度,跨过学习路上的各个槛。
## 步骤 3 : 我的订单页面操作
在我的订单页面,根据订单的不同状态,可以做出如下不同的操作:
1. 付款 —— 已经生成,但是未付款
2. 确认收货 —— 通过**后台发货**后
3. 评价 —— 确认收货后, 将在**评价产品**知识点讲解,这里只是提到
4. 删除 —— 任意状态下
![](https://box.kancloud.cn/1a2b76876086b852725546081a032631_1031x886.png)
## 步骤 4 : 付款
点击付款按钮直接跳转到在生成订单中的确认支付页环节,属于已经开发的功能,在此不作赘述。
![](https://box.kancloud.cn/b12acc81135a192f82fd40e472bf18cf_309x415.png)
## 步骤 5 : 确认收货
1. 点击确认收货后,访问地址`/foreconfirmPay`
2. `ForeController.confirmPay()`方法被调用
2.1 获取参数oid
2.2 通过oid获取订单对象o
2.3 为订单对象填充订单项
2.4 把订单对象放在request的属性"o"上
2.5 服务端跳转到 confirmPay.jsp
3. confirmPay.jsp
与 register.jsp 相仿,confirmPay.jsp也包含了header.jsp, top.jsp, simpleSearch.jsp, footer.jsp 等公共页面。
中间是订单确认业务页面 confirmPayPage.jsp
4. confirmPayPage.jsp
显示订单的创建时间,付款时间和发货时间,以及订单号,收款人信息等
遍历订单项集合,显示其中的产品图片,产品标题,价格,数量,小计,总结信息
最后提供确认支付按钮,提交到`/foreorderconfirmed`路径
ForeController.java:
~~~
package com.dodoke.tmall.controller;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;
import com.dodoke.tmall.comparator.ProductAllComparator;
import com.dodoke.tmall.comparator.ProductDateComparator;
import com.dodoke.tmall.comparator.ProductPriceComparator;
import com.dodoke.tmall.comparator.ProductReviewComparator;
import com.dodoke.tmall.comparator.ProductSaleCountComparator;
import com.dodoke.tmall.pojo.Category;
import com.dodoke.tmall.pojo.Order;
import com.dodoke.tmall.pojo.OrderItem;
import com.dodoke.tmall.pojo.Product;
import com.dodoke.tmall.pojo.ProductImage;
import com.dodoke.tmall.pojo.PropertyValue;
import com.dodoke.tmall.pojo.Review;
import com.dodoke.tmall.pojo.User;
import com.dodoke.tmall.service.CategoryService;
import com.dodoke.tmall.service.OrderItemService;
import com.dodoke.tmall.service.OrderService;
import com.dodoke.tmall.service.ProductImageService;
import com.dodoke.tmall.service.ProductService;
import com.dodoke.tmall.service.PropertyValueService;
import com.dodoke.tmall.service.ReviewService;
import com.dodoke.tmall.service.UserService;
import com.github.pagehelper.PageHelper;
@Controller
@RequestMapping("")
public class ForeController {
@Autowired
CategoryService categoryService;
@Autowired
ProductService productService;
@Autowired
UserService userService;
@Autowired
ProductImageService productImageService;
@Autowired
PropertyValueService propertyValueService;
@Autowired
OrderService orderService;
@Autowired
OrderItemService orderItemService;
@Autowired
ReviewService reviewService;
@RequestMapping("forehome")
public String home(Model model) {
List<Category> cs = categoryService.list();
productService.fill(cs);
productService.fillByRow(cs);
model.addAttribute("cs", cs);
return "fore/home";
}
@RequestMapping("foreregister")
public String register(Model model, User user) {
String name = user.getName();
// 把账号里的特殊符号进行转义
name = HtmlUtils.htmlEscape(name);
user.setName(name);
boolean exist = userService.isExist(name);
if (exist) {
String m = "用户名已经被使用,不能使用";
model.addAttribute("msg", m);
model.addAttribute("user", null);
return "fore/register";
}
userService.add(user);
return "redirect:registerSuccessPage";
}
@RequestMapping("forelogin")
public String login(@RequestParam("name") String name, @RequestParam("password") String password, Model model,
HttpSession session) {
name = HtmlUtils.htmlEscape(name);
User user = userService.get(name, password);
if (null == user) {
model.addAttribute("msg", "账号密码错误");
return "fore/login";
}
session.setAttribute("user", user);
return "redirect:forehome";
}
@RequestMapping("forelogout")
public String logout(HttpSession session) {
session.removeAttribute("user");
return "redirect:forehome";
}
@RequestMapping("foreproduct")
public String product(int pid, Model model) {
Product p = productService.get(pid);
// 根据对象p,获取这个产品对应的单个图片集合
List<ProductImage> productSingleImages = productImageService.list(p.getId(), ProductImageService.type_single);
// 根据对象p,获取这个产品对应的详情图片集合
List<ProductImage> productDetailImages = productImageService.list(p.getId(), ProductImageService.type_detail);
p.setProductSingleImages(productSingleImages);
p.setProductDetailImages(productDetailImages);
// 获取产品的所有属性值
List<PropertyValue> pvs = propertyValueService.list(p.getId());
// 获取产品对应的所有的评价
List<Review> reviews = reviewService.list(p.getId());
// 设置产品的销量和评价数量
productService.setSaleAndReviewNumber(p);
model.addAttribute("reviews", reviews);
model.addAttribute("p", p);
model.addAttribute("pvs", pvs);
return "fore/product";
}
@RequestMapping("forecheckLogin")
@ResponseBody
public String checkLogin(HttpSession session) {
User user = (User) session.getAttribute("user");
if (null != user) {
return "success";
}
return "fail";
}
@RequestMapping("foreloginAjax")
@ResponseBody
public String loginAjax(@RequestParam("name") String name, @RequestParam("password") String password,
HttpSession session) {
name = HtmlUtils.htmlEscape(name);
User user = userService.get(name, password);
if (null == user) {
return "fail";
}
session.setAttribute("user", user);
return "success";
}
@RequestMapping("forecategory")
public String category(int cid, String sort, Model model) {
Category c = categoryService.get(cid);
productService.fill(c);
productService.setSaleAndReviewNumber(c.getProducts());
if (null != sort) {
switch (sort) {
case "review":
Collections.sort(c.getProducts(), new ProductReviewComparator());
break;
case "date":
Collections.sort(c.getProducts(), new ProductDateComparator());
break;
case "saleCount":
Collections.sort(c.getProducts(), new ProductSaleCountComparator());
break;
case "price":
Collections.sort(c.getProducts(), new ProductPriceComparator());
break;
case "all":
Collections.sort(c.getProducts(), new ProductAllComparator());
break;
}
}
model.addAttribute("c", c);
return "fore/category";
}
@RequestMapping("foresearch")
public String search(String keyword, Model model) {
PageHelper.offsetPage(0, 20);
List<Product> ps = productService.search(keyword);
productService.setSaleAndReviewNumber(ps);
model.addAttribute("ps", ps);
return "fore/searchResult";
}
@RequestMapping("forebuyone")
public String buyone(int pid, int num, HttpSession session) {
Product p = productService.get(pid);
int oiid = 0;
User user = (User) session.getAttribute("user");
boolean found = false;
List<OrderItem> ois = orderItemService.listByUser(user.getId());
for (OrderItem oi : ois) {
if (oi.getProduct().getId().intValue() == p.getId().intValue()) {
oi.setNumber(oi.getNumber() + num);
orderItemService.update(oi);
found = true;
oiid = oi.getId();
break;
}
}
if (!found) {
OrderItem oi = new OrderItem();
oi.setUserId(user.getId());
oi.setNumber(num);
oi.setProductId(pid);
orderItemService.add(oi);
oiid = oi.getId();
}
return "redirect:forebuy?oiid=" + oiid;
}
@RequestMapping("forebuy")
public String buy(Model model, String[] oiid, HttpSession session) {
List<OrderItem> ois = new ArrayList<>();
float total = 0;
for (String strid : oiid) {
int id = Integer.parseInt(strid);
OrderItem oi = orderItemService.get(id);
total += oi.getProduct().getPromotePrice() * oi.getNumber();
ois.add(oi);
}
session.setAttribute("ois", ois);
model.addAttribute("total", total);
return "fore/buy";
}
@RequestMapping("foreaddCart")
@ResponseBody
public String addCart(int pid, int num, Model model, HttpSession session) {
Product p = productService.get(pid);
User user = (User) session.getAttribute("user");
boolean found = false;
List<OrderItem> ois = orderItemService.listByUser(user.getId());
for (OrderItem oi : ois) {
if (oi.getProduct().getId().intValue() == p.getId().intValue()) {
oi.setNumber(oi.getNumber() + num);
orderItemService.update(oi);
found = true;
break;
}
}
if (!found) {
OrderItem oi = new OrderItem();
oi.setUserId(user.getId());
oi.setNumber(num);
oi.setProductId(pid);
orderItemService.add(oi);
}
return "success";
}
@RequestMapping("forecart")
public String cart(Model model, HttpSession session) {
User user = (User) session.getAttribute("user");
List<OrderItem> ois = orderItemService.listByUser(user.getId());
model.addAttribute("ois", ois);
return "fore/cart";
}
@RequestMapping("forechangeOrderItem")
@ResponseBody
public String changeOrderItem(Model model, HttpSession session, int pid, int number) {
User user = (User) session.getAttribute("user");
if (null == user) {
return "fail";
}
List<OrderItem> ois = orderItemService.listByUser(user.getId());
for (OrderItem oi : ois) {
if (oi.getProduct().getId().intValue() == pid) {
oi.setNumber(number);
orderItemService.update(oi);
break;
}
}
return "success";
}
@RequestMapping("foredeleteOrderItem")
@ResponseBody
public String deleteOrderItem(Model model, HttpSession session, int oiid) {
User user = (User) session.getAttribute("user");
if (null == user) {
return "fail";
}
orderItemService.delete(oiid);
return "success";
}
/**
* 创建订单
*
* @param model
* @param order
* 订单对象
* @param session
* @return 页面路径字符串
*/
@RequestMapping("forecreateOrder")
public String createOrder(Model model, Order order, HttpSession session) {
User user = (User) session.getAttribute("user");
// 生成订单号:年月日时分秒毫秒+随机数
String orderCode = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + RandomUtils.nextInt(10000);
order.setOrderCode(orderCode);
order.setCreateDate(new Date());
order.setUserId(user.getId());
order.setStatus(OrderService.waitPay);
List<OrderItem> ois = (List<OrderItem>) session.getAttribute("ois");
float total = orderService.add(order, ois);
return "redirect:forealipay?oid=" + order.getId() + "&total=" + total;
}
@RequestMapping("forepayed")
public String payed(int oid, float total, Model model) {
Order order = orderService.get(oid);
order.setStatus(OrderService.waitDelivery);
order.setPayDate(new Date());
orderService.update(order);
model.addAttribute("o", order);
return "fore/payed";
}
@RequestMapping("forebought")
public String bought(Model model, HttpSession session) {
User user = (User) session.getAttribute("user");
List<Order> os = orderService.list(user.getId(), OrderService.delete);
orderItemService.fill(os);
model.addAttribute("os", os);
return "fore/bought";
}
@RequestMapping("foreconfirmPay")
public String confirmPay(Model model, int oid) {
Order o = orderService.get(oid);
orderItemService.fill(o);
model.addAttribute("o", o);
return "fore/confirmPay";
}
}
~~~
confirmPay.jsp:
~~~
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix='fmt' uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<div class="confirmPayPageDiv">
<div class="confirmPayImageDiv">
<img src="img/site/comformPayFlow.png">
<div class="confirmPayTime1">${o.createDate}</div>
<div class="confirmPayTime2">${o.payDate}</div>
<div class="confirmPayTime3">yyyy-MM-dd HH:mm:ss</div>
</div>
<div class="confirmPayOrderInfoDiv">
<div class="confirmPayOrderInfoText">我已收到货,同意支付宝付款</div>
</div>
<div class="confirmPayOrderItemDiv">
<div class="confirmPayOrderItemText">订单信息</div>
<table class="confirmPayOrderItemTable">
<thead>
<th colspan="2">宝贝</th>
<th width="120px">单价</th>
<th width="120px">数量</th>
<th width="120px">商品总价</th>
<th width="120px">运费</th>
</thead>
<c:forEach items="${o.orderItems}" var="oi">
<tr>
<td><img width="50px" src="img/productSingle_middle/${oi.product.firstProductImage.id}.jpg"></td>
<td class="confirmPayOrderItemProductLink"><a href="#nowhere">${oi.product.name}</a></td>
<td>¥<fmt:formatNumber type="number" value="${oi.product.originalPrice}" minFractionDigits="2" /></td>
<td>1</td>
<td><span class="conformPayProductPrice">¥<fmt:formatNumber type="number" value="${oi.product.promotePrice}" minFractionDigits="2" /></span></td>
<td><span>快递 : 0.00 </span></td>
</tr>
</c:forEach>
</table>
<div class="confirmPayOrderItemText pull-right">
实付款: <span class="confirmPayOrderItemSumPrice">¥<fmt:formatNumber type="number" value="${o.total}" minFractionDigits="2" /></span>
</div>
</div>
<div class="confirmPayOrderDetailDiv">
<table class="confirmPayOrderDetailTable">
<tr>
<td>订单编号:</td>
<td>${o.orderCode}<img width="23px" src="img/site/confirmOrderTmall.png"></td>
</tr>
<tr>
<td>卖家昵称:</td>
<td>天猫商铺 <span class="confirmPayOrderDetailWangWangGif"></span></td>
</tr>
<tr>
<td>收货信息:</td>
<td>${o.address},${o.receiver},${o.mobile},${o.post}</td>
</tr>
<tr>
<td>成交时间:</td>
<td>o.createDate</td>
</tr>
</table>
</div>
<div class="confirmPayButtonDiv">
<div class="confirmPayWarning">请收到货后,再确认收货!否则您可能钱货两空!</div>
<a href="foreorderConfirmed?oid=${o.id}"><button class="confirmPayButton">确认支付</button></a>
</div>
</div>
~~~
## 步骤 6 : 确认收货成功
通过上一步最后的确认支付按钮,提交到路径`/foreorderConfirmed`,导致`ForeController.orderConfirmed()`方法被调用
1. `ForeController.orderConfirmed()` 方法
1.1 获取参数oid
1.2 根据参数oid获取Order对象o
1.3 修改对象o的状态为等待评价,修改其确认支付时间
1.4 更新到数据库
1.5 服务端跳转到orderConfirmed.jsp页面
2. orderConfirmed.jsp
与 register.jsp 相仿,orderConfirmed.jsp也包含了header.jsp, top.jsp, simpleSearch.jsp, footer.jsp 等公共页面。
中间是确认收货成功业务页面 orderConfirmedPage.jsp
3. orderConfirmedPage.jsp
显示"交易已经成功,卖家将收到您的货款。"
orderConfirmed()方法:
~~~
@RequestMapping("foreorderConfirmed")
public String orderConfirmed(Model model, int oid) {
Order o = orderService.get(oid);
o.setStatus(OrderService.waitReview);
o.setConfirmDate(new Date());
orderService.update(o);
return "fore/orderConfirmed";
}
~~~
orderConfirmed.jsp:
~~~
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="../include/fore/header.jsp"%>
<%@include file="../include/fore/top.jsp"%>
<%@include file="../include/fore/simpleSearch.jsp"%>
<%@include file="../include/fore/cart/orderConfirmedPage.jsp"%>
<%@include file="../include/fore/footer.jsp"%>
~~~
orderConfirmedPage.jsp:
~~~
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<div class="orderFinishDiv">
<div class="orderFinishTextDiv">
<img src="img/site/orderFinish.png"> <span>交易已经成功,卖家将收到您的货款。</span>
</div>
</div>
~~~
ForeController.java
~~~
package com.dodoke.tmall.controller;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;
import com.dodoke.tmall.comparator.ProductAllComparator;
import com.dodoke.tmall.comparator.ProductDateComparator;
import com.dodoke.tmall.comparator.ProductPriceComparator;
import com.dodoke.tmall.comparator.ProductReviewComparator;
import com.dodoke.tmall.comparator.ProductSaleCountComparator;
import com.dodoke.tmall.pojo.Category;
import com.dodoke.tmall.pojo.Order;
import com.dodoke.tmall.pojo.OrderItem;
import com.dodoke.tmall.pojo.Product;
import com.dodoke.tmall.pojo.ProductImage;
import com.dodoke.tmall.pojo.PropertyValue;
import com.dodoke.tmall.pojo.Review;
import com.dodoke.tmall.pojo.User;
import com.dodoke.tmall.service.CategoryService;
import com.dodoke.tmall.service.OrderItemService;
import com.dodoke.tmall.service.OrderService;
import com.dodoke.tmall.service.ProductImageService;
import com.dodoke.tmall.service.ProductService;
import com.dodoke.tmall.service.PropertyValueService;
import com.dodoke.tmall.service.ReviewService;
import com.dodoke.tmall.service.UserService;
import com.github.pagehelper.PageHelper;
@Controller
@RequestMapping("")
public class ForeController {
@Autowired
CategoryService categoryService;
@Autowired
ProductService productService;
@Autowired
UserService userService;
@Autowired
ProductImageService productImageService;
@Autowired
PropertyValueService propertyValueService;
@Autowired
OrderService orderService;
@Autowired
OrderItemService orderItemService;
@Autowired
ReviewService reviewService;
@RequestMapping("forehome")
public String home(Model model) {
List<Category> cs = categoryService.list();
productService.fill(cs);
productService.fillByRow(cs);
model.addAttribute("cs", cs);
return "fore/home";
}
@RequestMapping("foreregister")
public String register(Model model, User user) {
String name = user.getName();
// 把账号里的特殊符号进行转义
name = HtmlUtils.htmlEscape(name);
user.setName(name);
boolean exist = userService.isExist(name);
if (exist) {
String m = "用户名已经被使用,不能使用";
model.addAttribute("msg", m);
model.addAttribute("user", null);
return "fore/register";
}
userService.add(user);
return "redirect:registerSuccessPage";
}
@RequestMapping("forelogin")
public String login(@RequestParam("name") String name, @RequestParam("password") String password, Model model,
HttpSession session) {
name = HtmlUtils.htmlEscape(name);
User user = userService.get(name, password);
if (null == user) {
model.addAttribute("msg", "账号密码错误");
return "fore/login";
}
session.setAttribute("user", user);
return "redirect:forehome";
}
@RequestMapping("forelogout")
public String logout(HttpSession session) {
session.removeAttribute("user");
return "redirect:forehome";
}
@RequestMapping("foreproduct")
public String product(int pid, Model model) {
Product p = productService.get(pid);
// 根据对象p,获取这个产品对应的单个图片集合
List<ProductImage> productSingleImages = productImageService.list(p.getId(), ProductImageService.type_single);
// 根据对象p,获取这个产品对应的详情图片集合
List<ProductImage> productDetailImages = productImageService.list(p.getId(), ProductImageService.type_detail);
p.setProductSingleImages(productSingleImages);
p.setProductDetailImages(productDetailImages);
// 获取产品的所有属性值
List<PropertyValue> pvs = propertyValueService.list(p.getId());
// 获取产品对应的所有的评价
List<Review> reviews = reviewService.list(p.getId());
// 设置产品的销量和评价数量
productService.setSaleAndReviewNumber(p);
model.addAttribute("reviews", reviews);
model.addAttribute("p", p);
model.addAttribute("pvs", pvs);
return "fore/product";
}
@RequestMapping("forecheckLogin")
@ResponseBody
public String checkLogin(HttpSession session) {
User user = (User) session.getAttribute("user");
if (null != user) {
return "success";
}
return "fail";
}
@RequestMapping("foreloginAjax")
@ResponseBody
public String loginAjax(@RequestParam("name") String name, @RequestParam("password") String password,
HttpSession session) {
name = HtmlUtils.htmlEscape(name);
User user = userService.get(name, password);
if (null == user) {
return "fail";
}
session.setAttribute("user", user);
return "success";
}
@RequestMapping("forecategory")
public String category(int cid, String sort, Model model) {
Category c = categoryService.get(cid);
productService.fill(c);
productService.setSaleAndReviewNumber(c.getProducts());
if (null != sort) {
switch (sort) {
case "review":
Collections.sort(c.getProducts(), new ProductReviewComparator());
break;
case "date":
Collections.sort(c.getProducts(), new ProductDateComparator());
break;
case "saleCount":
Collections.sort(c.getProducts(), new ProductSaleCountComparator());
break;
case "price":
Collections.sort(c.getProducts(), new ProductPriceComparator());
break;
case "all":
Collections.sort(c.getProducts(), new ProductAllComparator());
break;
}
}
model.addAttribute("c", c);
return "fore/category";
}
@RequestMapping("foresearch")
public String search(String keyword, Model model) {
PageHelper.offsetPage(0, 20);
List<Product> ps = productService.search(keyword);
productService.setSaleAndReviewNumber(ps);
model.addAttribute("ps", ps);
return "fore/searchResult";
}
@RequestMapping("forebuyone")
public String buyone(int pid, int num, HttpSession session) {
Product p = productService.get(pid);
int oiid = 0;
User user = (User) session.getAttribute("user");
boolean found = false;
List<OrderItem> ois = orderItemService.listByUser(user.getId());
for (OrderItem oi : ois) {
if (oi.getProduct().getId().intValue() == p.getId().intValue()) {
oi.setNumber(oi.getNumber() + num);
orderItemService.update(oi);
found = true;
oiid = oi.getId();
break;
}
}
if (!found) {
OrderItem oi = new OrderItem();
oi.setUserId(user.getId());
oi.setNumber(num);
oi.setProductId(pid);
orderItemService.add(oi);
oiid = oi.getId();
}
return "redirect:forebuy?oiid=" + oiid;
}
@RequestMapping("forebuy")
public String buy(Model model, String[] oiid, HttpSession session) {
List<OrderItem> ois = new ArrayList<>();
float total = 0;
for (String strid : oiid) {
int id = Integer.parseInt(strid);
OrderItem oi = orderItemService.get(id);
total += oi.getProduct().getPromotePrice() * oi.getNumber();
ois.add(oi);
}
session.setAttribute("ois", ois);
model.addAttribute("total", total);
return "fore/buy";
}
@RequestMapping("foreaddCart")
@ResponseBody
public String addCart(int pid, int num, Model model, HttpSession session) {
Product p = productService.get(pid);
User user = (User) session.getAttribute("user");
boolean found = false;
List<OrderItem> ois = orderItemService.listByUser(user.getId());
for (OrderItem oi : ois) {
if (oi.getProduct().getId().intValue() == p.getId().intValue()) {
oi.setNumber(oi.getNumber() + num);
orderItemService.update(oi);
found = true;
break;
}
}
if (!found) {
OrderItem oi = new OrderItem();
oi.setUserId(user.getId());
oi.setNumber(num);
oi.setProductId(pid);
orderItemService.add(oi);
}
return "success";
}
@RequestMapping("forecart")
public String cart(Model model, HttpSession session) {
User user = (User) session.getAttribute("user");
List<OrderItem> ois = orderItemService.listByUser(user.getId());
model.addAttribute("ois", ois);
return "fore/cart";
}
@RequestMapping("forechangeOrderItem")
@ResponseBody
public String changeOrderItem(Model model, HttpSession session, int pid, int number) {
User user = (User) session.getAttribute("user");
if (null == user) {
return "fail";
}
List<OrderItem> ois = orderItemService.listByUser(user.getId());
for (OrderItem oi : ois) {
if (oi.getProduct().getId().intValue() == pid) {
oi.setNumber(number);
orderItemService.update(oi);
break;
}
}
return "success";
}
@RequestMapping("foredeleteOrderItem")
@ResponseBody
public String deleteOrderItem(Model model, HttpSession session, int oiid) {
User user = (User) session.getAttribute("user");
if (null == user) {
return "fail";
}
orderItemService.delete(oiid);
return "success";
}
/**
* 创建订单
*
* @param model
* @param order
* 订单对象
* @param session
* @return 页面路径字符串
*/
@RequestMapping("forecreateOrder")
public String createOrder(Model model, Order order, HttpSession session) {
User user = (User) session.getAttribute("user");
// 生成订单号:年月日时分秒毫秒+随机数
String orderCode = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + RandomUtils.nextInt(10000);
order.setOrderCode(orderCode);
order.setCreateDate(new Date());
order.setUserId(user.getId());
order.setStatus(OrderService.waitPay);
List<OrderItem> ois = (List<OrderItem>) session.getAttribute("ois");
float total = orderService.add(order, ois);
return "redirect:forealipay?oid=" + order.getId() + "&total=" + total;
}
@RequestMapping("forepayed")
public String payed(int oid, float total, Model model) {
Order order = orderService.get(oid);
order.setStatus(OrderService.waitDelivery);
order.setPayDate(new Date());
orderService.update(order);
model.addAttribute("o", order);
return "fore/payed";
}
@RequestMapping("forebought")
public String bought(Model model, HttpSession session) {
User user = (User) session.getAttribute("user");
List<Order> os = orderService.list(user.getId(), OrderService.delete);
orderItemService.fill(os);
model.addAttribute("os", os);
return "fore/bought";
}
@RequestMapping("foreconfirmPay")
public String confirmPay(Model model, int oid) {
Order o = orderService.get(oid);
orderItemService.fill(o);
model.addAttribute("o", o);
return "fore/confirmPay";
}
@RequestMapping("foreorderConfirmed")
public String orderConfirmed(Model model, int oid) {
Order o = orderService.get(oid);
o.setStatus(OrderService.waitReview);
o.setConfirmDate(new Date());
orderService.update(o);
return "fore/orderConfirmed";
}
}
~~~
## 步骤 7 : 评价
点击评价按钮,跳转到路径`/forereview`,这部分单独开了个知识点**评价产品**进行讲解。
## 步骤 8 : 删除
在我的订单页 上点击删除按钮,根据 boughtPage.jsp 中的ajax操作,会访问路径/foredeleteOrder,导致ForeController.deleteOrder方法被调用
1. ForeController.deleteOrder()
1.1 获取参数oid
1.2 根据oid获取订单对象o
1.3 修改状态
1.4 更新到数据库
1.5 返回字符串"success"
2. boughtPage.jsp 中的javascript代码获取返回字符串是success的时候,移除掉当前这行订单数据。
![](https://box.kancloud.cn/e913d643a1ec9779ac0d4324e63e82db_398x151.png)
~~~
var page = "foredeleteOrder";
$.post(page, {
"oid" : deleteOrderid
}, function(result) {
if ("success" == result) {
$("table.orderListItemTable[oid=" + deleteOrderid + "]").remove();
} else {
location.href = "login.jsp";
}
});
~~~
deleteOrder方法:
~~~
@RequestMapping("foredeleteOrder")
@ResponseBody
public String deleteOrder(Model model, int oid) {
Order o = orderService.get(oid);
o.setStatus(OrderService.delete);
orderService.update(o);
return "success";
}
~~~
ForeController:
~~~
package com.dodoke.tmall.controller;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;
import com.dodoke.tmall.comparator.ProductAllComparator;
import com.dodoke.tmall.comparator.ProductDateComparator;
import com.dodoke.tmall.comparator.ProductPriceComparator;
import com.dodoke.tmall.comparator.ProductReviewComparator;
import com.dodoke.tmall.comparator.ProductSaleCountComparator;
import com.dodoke.tmall.pojo.Category;
import com.dodoke.tmall.pojo.Order;
import com.dodoke.tmall.pojo.OrderItem;
import com.dodoke.tmall.pojo.Product;
import com.dodoke.tmall.pojo.ProductImage;
import com.dodoke.tmall.pojo.PropertyValue;
import com.dodoke.tmall.pojo.Review;
import com.dodoke.tmall.pojo.User;
import com.dodoke.tmall.service.CategoryService;
import com.dodoke.tmall.service.OrderItemService;
import com.dodoke.tmall.service.OrderService;
import com.dodoke.tmall.service.ProductImageService;
import com.dodoke.tmall.service.ProductService;
import com.dodoke.tmall.service.PropertyValueService;
import com.dodoke.tmall.service.ReviewService;
import com.dodoke.tmall.service.UserService;
import com.github.pagehelper.PageHelper;
@Controller
@RequestMapping("")
public class ForeController {
@Autowired
CategoryService categoryService;
@Autowired
ProductService productService;
@Autowired
UserService userService;
@Autowired
ProductImageService productImageService;
@Autowired
PropertyValueService propertyValueService;
@Autowired
OrderService orderService;
@Autowired
OrderItemService orderItemService;
@Autowired
ReviewService reviewService;
@RequestMapping("forehome")
public String home(Model model) {
List<Category> cs = categoryService.list();
productService.fill(cs);
productService.fillByRow(cs);
model.addAttribute("cs", cs);
return "fore/home";
}
@RequestMapping("foreregister")
public String register(Model model, User user) {
String name = user.getName();
// 把账号里的特殊符号进行转义
// 在恶意注册的时候,会使用诸如 <script>alert('papapa')</script>
// 这样的名称,会导致网页打开就弹出一个对话框。
// 那么在转义之后,就没有这个问题了。
name = HtmlUtils.htmlEscape(name);
user.setName(name);
boolean exist = userService.isExist(name);
if (exist) {
String m = "用户名已经被使用,不能使用";
model.addAttribute("msg", m);
model.addAttribute("user", null);
return "fore/register";
}
userService.add(user);
return "redirect:registerSuccessPage";
}
@RequestMapping("forelogin")
public String login(@RequestParam("name") String name, @RequestParam("password") String password, Model model,
HttpSession session) {
name = HtmlUtils.htmlEscape(name);
User user = userService.get(name, password);
if (null == user) {
model.addAttribute("msg", "账号密码错误");
return "fore/login";
}
session.setAttribute("user", user);
return "redirect:forehome";
}
@RequestMapping("forelogout")
public String logout(HttpSession session) {
session.removeAttribute("user");
return "redirect:forehome";
}
@RequestMapping("foreproduct")
public String product(int pid, Model model) {
Product p = productService.get(pid);
// 根据对象p,获取这个产品对应的单个图片集合
List<ProductImage> productSingleImages = productImageService.list(p.getId(), ProductImageService.type_single);
// 根据对象p,获取这个产品对应的详情图片集合
List<ProductImage> productDetailImages = productImageService.list(p.getId(), ProductImageService.type_detail);
p.setProductSingleImages(productSingleImages);
p.setProductDetailImages(productDetailImages);
// 获取产品的所有属性值
List<PropertyValue> pvs = propertyValueService.list(p.getId());
// 获取产品对应的所有的评价
List<Review> reviews = reviewService.list(p.getId());
// 设置产品的销量和评价数量
productService.setSaleAndReviewNumber(p);
model.addAttribute("reviews", reviews);
model.addAttribute("p", p);
model.addAttribute("pvs", pvs);
return "fore/product";
}
@RequestMapping("forecheckLogin")
@ResponseBody
public String checkLogin(HttpSession session) {
User user = (User) session.getAttribute("user");
if (null != user) {
return "success";
}
return "fail";
}
@RequestMapping("foreloginAjax")
@ResponseBody
public String loginAjax(@RequestParam("name") String name, @RequestParam("password") String password,
HttpSession session) {
name = HtmlUtils.htmlEscape(name);
User user = userService.get(name, password);
if (null == user) {
return "fail";
}
session.setAttribute("user", user);
return "success";
}
@RequestMapping("forecategory")
public String category(int cid, String sort, Model model) {
Category c = categoryService.get(cid);
productService.fill(c);
productService.setSaleAndReviewNumber(c.getProducts());
if (null != sort) {
switch (sort) {
case "review":
Collections.sort(c.getProducts(), new ProductReviewComparator());
break;
case "date":
Collections.sort(c.getProducts(), new ProductDateComparator());
break;
case "saleCount":
Collections.sort(c.getProducts(), new ProductSaleCountComparator());
break;
case "price":
Collections.sort(c.getProducts(), new ProductPriceComparator());
break;
case "all":
Collections.sort(c.getProducts(), new ProductAllComparator());
break;
}
}
model.addAttribute("c", c);
return "fore/category";
}
@RequestMapping("foresearch")
public String search(String keyword, Model model) {
PageHelper.offsetPage(0, 20);
List<Product> ps = productService.search(keyword);
productService.setSaleAndReviewNumber(ps);
model.addAttribute("ps", ps);
return "fore/searchResult";
}
@RequestMapping("forebuyone")
public String buyone(int pid, int num, HttpSession session) {
Product p = productService.get(pid);
int oiid = 0;
User user = (User) session.getAttribute("user");
boolean found = false;
List<OrderItem> ois = orderItemService.listByUser(user.getId());
for (OrderItem oi : ois) {
if (oi.getProduct().getId().intValue() == p.getId().intValue()) {
oi.setNumber(oi.getNumber() + num);
orderItemService.update(oi);
found = true;
oiid = oi.getId();
break;
}
}
if (!found) {
OrderItem oi = new OrderItem();
oi.setUserId(user.getId());
oi.setNumber(num);
oi.setProductId(pid);
orderItemService.add(oi);
oiid = oi.getId();
}
return "redirect:forebuy?oiid=" + oiid;
}
@RequestMapping("forebuy")
public String buy(Model model, String[] oiid, HttpSession session) {
List<OrderItem> ois = new ArrayList<>();
float total = 0;
for (String strid : oiid) {
int id = Integer.parseInt(strid);
OrderItem oi = orderItemService.get(id);
total += oi.getProduct().getPromotePrice() * oi.getNumber();
ois.add(oi);
}
session.setAttribute("ois", ois);
model.addAttribute("total", total);
return "fore/buy";
}
@RequestMapping("foreaddCart")
@ResponseBody
public String addCart(int pid, int num, Model model, HttpSession session) {
Product p = productService.get(pid);
User user = (User) session.getAttribute("user");
boolean found = false;
List<OrderItem> ois = orderItemService.listByUser(user.getId());
for (OrderItem oi : ois) {
if (oi.getProduct().getId().intValue() == p.getId().intValue()) {
oi.setNumber(oi.getNumber() + num);
orderItemService.update(oi);
found = true;
break;
}
}
if (!found) {
OrderItem oi = new OrderItem();
oi.setUserId(user.getId());
oi.setNumber(num);
oi.setProductId(pid);
orderItemService.add(oi);
}
return "success";
}
@RequestMapping("forecart")
public String cart(Model model, HttpSession session) {
User user = (User) session.getAttribute("user");
List<OrderItem> ois = orderItemService.listByUser(user.getId());
model.addAttribute("ois", ois);
return "fore/cart";
}
@RequestMapping("forechangeOrderItem")
@ResponseBody
public String changeOrderItem(Model model, HttpSession session, int pid, int number) {
User user = (User) session.getAttribute("user");
if (null == user) {
return "fail";
}
List<OrderItem> ois = orderItemService.listByUser(user.getId());
for (OrderItem oi : ois) {
if (oi.getProduct().getId().intValue() == pid) {
oi.setNumber(number);
orderItemService.update(oi);
break;
}
}
return "success";
}
@RequestMapping("foredeleteOrderItem")
@ResponseBody
public String deleteOrderItem(Model model, HttpSession session, int oiid) {
User user = (User) session.getAttribute("user");
if (null == user) {
return "fail";
}
orderItemService.delete(oiid);
return "success";
}
/**
* 创建订单
*
* @param model
* @param order
* 订单对象
* @param session
* @return 页面路径字符串
*/
@RequestMapping("forecreateOrder")
public String createOrder(Model model, Order order, HttpSession session) {
User user = (User) session.getAttribute("user");
// 生成订单号:年月日时分秒毫秒+随机数
String orderCode = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + RandomUtils.nextInt(10000);
order.setOrderCode(orderCode);
order.setCreateDate(new Date());
order.setUserId(user.getId());
order.setStatus(OrderService.waitPay);
List<OrderItem> ois = (List<OrderItem>) session.getAttribute("ois");
float total = orderService.add(order, ois);
return "redirect:forealipay?oid=" + order.getId() + "&total=" + total;
}
@RequestMapping("forepayed")
public String payed(int oid, float total, Model model) {
Order order = orderService.get(oid);
order.setStatus(OrderService.waitDelivery);
order.setPayDate(new Date());
orderService.update(order);
model.addAttribute("o", order);
return "fore/payed";
}
@RequestMapping("forebought")
public String bought(Model model, HttpSession session) {
User user = (User) session.getAttribute("user");
List<Order> os = orderService.list(user.getId(), OrderService.delete);
orderItemService.fill(os);
model.addAttribute("os", os);
return "fore/bought";
}
@RequestMapping("foreconfirmPay")
public String confirmPay(Model model, int oid) {
Order o = orderService.get(oid);
orderItemService.fill(o);
model.addAttribute("o", o);
return "fore/confirmPay";
}
@RequestMapping("foreorderConfirmed")
public String orderConfirmed(Model model, int oid) {
Order o = orderService.get(oid);
o.setStatus(OrderService.waitReview);
o.setConfirmDate(new Date());
orderService.update(o);
return "fore/orderConfirmed";
}
@RequestMapping("foredeleteOrder")
@ResponseBody
public String deleteOrder(Model model, int oid) {
Order o = orderService.get(oid);
o.setStatus(OrderService.delete);
orderService.update(o);
return "success";
}
}
~~~
- 项目简介
- 功能一览
- 前台
- 后台
- 开发流程
- 需求分析-展示
- 首页
- 产品页
- 分类页
- 搜索结果页
- 购物车查看页
- 结算页
- 确认支付页
- 支付成功页
- 我的订单页
- 确认收货页
- 确认收货成功页
- 评价页
- 需求分析-交互
- 分类页排序
- 立即购买
- 加入购物车
- 调整订单项数量
- 删除订单项
- 生成订单
- 订单页功能
- 确认付款
- 确认收货
- 提交评价信息
- 登录
- 注册
- 退出
- 搜索
- 前台需求列表
- 需求分析后台
- 分类管理
- 属性管理
- 产品管理
- 产品图片管理
- 产品属性设置
- 用户管理
- 订单管理
- 后台需求列表
- 表结构设计
- 数据建模
- 表与表之间的关系
- 后台-分类管理
- 可运行的项目
- 静态资源
- JSP包含关系
- 查询
- 分页
- 增加
- 删除
- 编辑
- 修改
- 做一遍
- 重构
- 分页方式
- 分类逆向工程
- 所有逆向工程
- 后台其他页面
- 属性管理实现
- 产品管理实现
- 产品图片管理实现
- 产品属性值设置
- 用户管理实现
- 订单管理实现
- 前端
- 前台-首页
- 可运行的项目
- 静态资源
- ForeController
- home方法
- home.jsp
- homePage.jsp
- 前台-无需登录
- 注册
- 登录
- 退出
- 产品页
- 模态登录
- 分类页
- 搜索
- 前台-需要登录
- 购物流程
- 立即购买
- 结算页面
- 加入购物车
- 查看购物车页面
- 登录状态拦截器
- 其他拦截器
- 购物车页面操作
- 订单状态图
- 生成订单
- 我的订单页
- 我的订单页操作
- 评价产品
- 总结