[TOC]
## 步骤 1 : 先运行,看到效果,再学习
先将完整的 tmall_ssm 项目(向老师要相关资料),配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
## 步骤 2 : 模仿和排错
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较**正确答案** ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,**学习有效果,排错有效率**,可以较为明显地提升学习速度,跨过学习路上的各个槛。
## 步骤 3 : 我的订单页
![](https://box.kancloud.cn/b458c0a41ecaa6dbd33bd5f16a1ee172_1299x795.png)
## 步骤 4 : OrderService
修改OrderService,新增方法
`List list(int uid, String excludedStatus);`
~~~
package com.dodoke.tmall.service;
import java.util.List;
import com.dodoke.tmall.pojo.Order;
import com.dodoke.tmall.pojo.OrderItem;
public interface OrderService {
String waitPay = "waitPay";
String waitDelivery = "waitDelivery";
String waitConfirm = "waitConfirm";
String waitReview = "waitReview";
String finish = "finish";
String delete = "delete";
void add(Order c);
void delete(int id);
void update(Order c);
Order get(int id);
List list();
List list(int uid, String excludedStatus);
}
~~~
## 步骤 5 : OrderServiceImpl
修改OrderServiceImpl,实现方法
`List list(int uid, String excludedStatus);`
~~~
package com.dodoke.tmall.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dodoke.tmall.mapper.OrderMapper;
import com.dodoke.tmall.pojo.Order;
import com.dodoke.tmall.pojo.OrderExample;
import com.dodoke.tmall.pojo.User;
import com.dodoke.tmall.service.OrderService;
import com.dodoke.tmall.service.UserService;
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
OrderMapper orderMapper;
@Autowired
UserService userService;
@Override
public void add(Order c) {
orderMapper.insert(c);
}
@Override
public void delete(int id) {
orderMapper.deleteByPrimaryKey(id);
}
@Override
public void update(Order c) {
orderMapper.updateByPrimaryKeySelective(c);
}
@Override
public Order get(int id) {
return orderMapper.selectByPrimaryKey(id);
}
public List<Order> list() {
OrderExample example = new OrderExample();
example.setOrderByClause("id desc");
List<Order> result = orderMapper.selectByExample(example);
setUser(result);
return result;
}
public void setUser(List<Order> os) {
for (Order o : os) {
setUser(o);
}
}
public void setUser(Order o) {
int uid = o.getUserId();
User u = userService.get(uid);
o.setUser(u);
}
@Override
public List list(int uid, String excludedStatus) {
OrderExample example = new OrderExample();
example.createCriteria().andUserIdEqualTo(uid).andStatusNotEqualTo(excludedStatus);
example.setOrderByClause("id desc");
return orderMapper.selectByExample(example);
}
}
~~~
## 步骤 6 : ForeController.bought()
/forebought导致ForeController.bought()方法被调用
1. 通过session获取用户user
2. 查询user所有的状态不是"delete" 的订单集合os
3. 为这些订单填充订单项
4. 把os放在model的属性"os"上
5. 服务端跳转到bought.jsp
~~~
package com.dodoke.tmall.controller;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpSession;
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";
}
@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";
}
}
~~~
## 步骤 7 : bought.jsp
与 register.jsp 相仿,bought.jsp 也包含了header.jsp, top.jsp, simpleSearch.jsp, footer.jsp 等公共页面。
中间是我的订单页面 boughtPage.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/boughtPage.jsp"%>
<%@include file="../include/fore/footer.jsp"%>
~~~
## 步骤 8 : boughtPage.jsp
在boughtPage.jsp中,做二次遍历
1. 遍历订单集合os
取出每个订单,显示其创建日期,订单号,总数量和总金额等
2. 遍历每个订单下的订单项集合o.orderItemsd
显示每个订单项对应的产品的图片,标题,原始价格,优惠价格等
### 交互效果js代码讲解
~~~
$("a[orderStatus]").click(function() {
var orderStatus = $(this).attr("orderStatus");
if ('all' == orderStatus) {
$("table[orderStatus]").show();
} else {
$("table[orderStatus]").hide();
$("table[orderStatus=" + orderStatus + "]").show();
}
$("div.orderType div").removeClass("selectedOrderType");
$(this).parent("div").addClass("selectedOrderType");
});
~~~
每一个订单类型超链都有一个自定义属性orderStatus,并且对应不同的值
~~~
<div class="selectedOrderType"><a href="#nowhere" orderstatus="all">所有订单</a></div>
<div class=""><a href="#nowhere" orderstatus="waitPay">待付款</a></div>
<div class=""><a href="#nowhere" orderstatus="waitDelivery">待发货</a></div>
<div class=""><a href="#nowhere" orderstatus="waitConfirm">待收货</a></div>
~~~
通过选择器
~~~
$("a[orderStatus]")
~~~
就能获取订单类型的超链
在超链的click事件中,获取自定义属性orderStatus值,判断点击的是哪个超链
接着对于每个订单所对应的table,也有一个orderStatus属性
~~~
<table orderstatus="waitReview" class="orderListItemTable" >
~~~
然后进行逻辑判断
当orderStatus是all的时候,那么就显示所有的table
~~~
$("table[orderStatus]").show();
~~~
当orderStatus不是all的时候,先把所有的table隐藏,然后把orderStatus对应的table显示出来
~~~
$("table[orderStatus="+orderStatus+"]").show();
~~~
最后去掉原来的超链所在div的选中状态,为当前的超链所在div加上选中状态
~~~
$("div.orderType div").removeClass("selectedOrderType");
$(this).parent("div").addClass("selectedOrderType");
~~~
> 补充说明:日期格式化,可以在getter方法中,写格式化操作,也可以使用fmt标签格式化,学员自己完成。
- 项目简介
- 功能一览
- 前台
- 后台
- 开发流程
- 需求分析-展示
- 首页
- 产品页
- 分类页
- 搜索结果页
- 购物车查看页
- 结算页
- 确认支付页
- 支付成功页
- 我的订单页
- 确认收货页
- 确认收货成功页
- 评价页
- 需求分析-交互
- 分类页排序
- 立即购买
- 加入购物车
- 调整订单项数量
- 删除订单项
- 生成订单
- 订单页功能
- 确认付款
- 确认收货
- 提交评价信息
- 登录
- 注册
- 退出
- 搜索
- 前台需求列表
- 需求分析后台
- 分类管理
- 属性管理
- 产品管理
- 产品图片管理
- 产品属性设置
- 用户管理
- 订单管理
- 后台需求列表
- 表结构设计
- 数据建模
- 表与表之间的关系
- 后台-分类管理
- 可运行的项目
- 静态资源
- JSP包含关系
- 查询
- 分页
- 增加
- 删除
- 编辑
- 修改
- 做一遍
- 重构
- 分页方式
- 分类逆向工程
- 所有逆向工程
- 后台其他页面
- 属性管理实现
- 产品管理实现
- 产品图片管理实现
- 产品属性值设置
- 用户管理实现
- 订单管理实现
- 前端
- 前台-首页
- 可运行的项目
- 静态资源
- ForeController
- home方法
- home.jsp
- homePage.jsp
- 前台-无需登录
- 注册
- 登录
- 退出
- 产品页
- 模态登录
- 分类页
- 搜索
- 前台-需要登录
- 购物流程
- 立即购买
- 结算页面
- 加入购物车
- 查看购物车页面
- 登录状态拦截器
- 其他拦截器
- 购物车页面操作
- 订单状态图
- 生成订单
- 我的订单页
- 我的订单页操作
- 评价产品
- 总结