企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 步骤 1 : 先运行,看到效果,再学习 先将完整的 tmall_ssm 项目(向老师要相关资料),配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。 ## 步骤 2 : 模仿和排错 在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。 模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较**正确答案** ( 可运行项目 ) 和自己的代码,来定位问题所在。 采用这种方式,**学习有效果,排错有效率**,可以较为明显地提升学习速度,跨过学习路上的各个槛。 ## 步骤 3 : 界面效果 ![](https://box.kancloud.cn/745187b9bed84d206d47de18120367b4_954x885.png) ## 步骤 4 : 评价产品页面 通过点击评价按钮,来到路径`/forereview`,导致ForeController.review()方法被调用 1. ForeController.review() 1.1 获取参数oid 1.2 根据oid获取订单对象o 1.3 为订单对象填充订单项 1.4 获取第一个订单项对应的产品,因为在评价页面需要显示一个产品图片,那么就使用这第一个产品的图片了 1.5 获取这个产品的评价集合 1.6 为产品设置评价数量和销量 1.7 把产品,订单和评价集合放在request上 1.8 服务端跳转到 review.jsp 2. review.jsp 与 register.jsp 相仿,review.jsp也包含了header.jsp, top.jsp, simpleSearch.jsp, footer.jsp 等公共页面。 中间是产品业务页面 reviewPage.jsp 3. reviewPage.jsp 在reviewPage.jsp中显示产品图片,产品标题,价格,产品销量,产品评价数量,以及订单信息等。 同时还显示出了该产品所有的评价,但是默认是隐藏的 ForeController.review(): ~~~ @RequestMapping("forereview") public String review(Model model, int oid) { Order o = orderService.get(oid); orderItemService.fill(o); Product p = o.getOrderItems().get(0).getProduct(); List<Review> reviews = reviewService.list(p.getId()); productService.setSaleAndReviewNumber(p); model.addAttribute("p", p); model.addAttribute("o", o); model.addAttribute("reviews", reviews); return "fore/review"; } ~~~ review.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/reviewPage.jsp"%> <%@include file="../include/fore/footer.jsp"%> ~~~ reviewPage.jsp: ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <div class="reviewDiv"> <div class="reviewProductInfoDiv"> <div class="reviewProductInfoImg"> <img width="400px" height="400px" src="img/productSingle/${p.firstProductImage.id}.jpg"> </div> <div class="reviewProductInfoRightDiv"> <div class="reviewProductInfoRightText">${p.name}</div> <table class="reviewProductInfoTable"> <tr> <td width="75px">价格:</td> <td><span class="reviewProductInfoTablePrice"> ¥<fmt:formatNumber type="number" value="${p.originalPrice}" minFractionDigits="2" /></span> 元</td> </tr> <tr> <td>配送</td> <td>快递: 0.00</td> </tr> <tr> <td>月销量:</td> <td><span class="reviewProductInfoTableSellNumber">${p.saleCount}</span> 件</td> </tr> </table> <div class="reviewProductInfoRightBelowDiv"> <span class="reviewProductInfoRightBelowImg"><img1 src="img/site/reviewLight.png"></span> <span class="reviewProductInfoRightBelowText">现在查看的是 您所购买商品的信息 于 ${o.createDate}下单购买了此商品 </span> </div> </div> <div style="clear: both"></div> </div> <div class="reviewStasticsDiv"> <div class="reviewStasticsLeft"> <div class="reviewStasticsLeftTop"></div> <div class="reviewStasticsLeftContent"> 累计评价 <span class="reviewStasticsNumber"> ${p.reviewCount}</span> </div> <div class="reviewStasticsLeftFoot"></div> </div> <div class="reviewStasticsRight"> <div class="reviewStasticsRightEmpty"></div> <div class="reviewStasticsFoot"></div> </div> </div> <c:if test="${param.showonly==true}"> <div class="reviewDivlistReviews"> <c:forEach items="${reviews}" var="r"> <div class="reviewDivlistReviewsEach"> <div class="reviewDate"> ${r.createDate} </div> <div class="reviewContent">${r.content}</div> <div class="reviewUserInfo pull-right">${r.user.anonymousName}<span class="reviewUserInfoAnonymous">(匿名)</span> </div> </div> </c:forEach> </div> </c:if> <c:if test="${param.showonly!=true}"> <div class="makeReviewDiv"> <form method="post" action="foredoreview"> <div class="makeReviewText">其他买家,需要你的建议哦!</div> <table class="makeReviewTable"> <tr> <td class="makeReviewTableFirstTD">评价商品</td> <td><textarea name="content"></textarea></td> </tr> </table> <div class="makeReviewButtonDiv"> <input type="hidden" name="oid" value="${o.id}"> <input type="hidden" name="pid" value="${p.id}"> <button type="submit">提交评价</button> </div> </form> </div> </c:if> </div> ~~~ 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(); // 把账号里的特殊符号进行转义 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"; } @RequestMapping("forereview") public String review(Model model, int oid) { Order o = orderService.get(oid); orderItemService.fill(o); Product p = o.getOrderItems().get(0).getProduct(); List<Review> reviews = reviewService.list(p.getId()); productService.setSaleAndReviewNumber(p); model.addAttribute("p", p); model.addAttribute("o", o); model.addAttribute("reviews", reviews); return "fore/review"; } } ~~~ ![](https://box.kancloud.cn/a829a61544985dede01cb3ffff8a09bb_1035x791.png) ## 步骤 5 : 提交评价 在评价产品页面点击提交评价,就把数据提交到了`/foredoreview`路径,导致`ForeController.doreview`方法被调用 1. `ForeController.doreview()` 1.1 获取参数oid 1.2 根据oid获取订单对象o 1.3 修改订单对象状态 1.4 更新订单对象到数据库 1.5 获取参数pid 1.6 根据pid获取产品对象 1.7 获取参数content (评价信息) 1.8 对评价信息进行转义,道理同注册`ForeController.register()` 1.9 从session中获取当前用户 1.10 创建评价对象review 1.11 为评价对象review设置 评价信息,产品,时间,用户 1.12 增加到数据库 1.13.客户端跳转到`/forereview`: 评价产品页面,并带上参数`showonly=true` 2. reviewPage.jsp 在reviewPage.jsp中,当参数showonly==true,那么就显示当前产品的所有评价信息 ![](https://box.kancloud.cn/d283a1d8c898025685593044ba9ddb56_1048x665.png) ~~~ @RequestMapping("foredoreview") public String doreview(Model model, HttpSession session, @RequestParam("oid") int oid, @RequestParam("pid") int pid, String content) { Order o = orderService.get(oid); o.setStatus(OrderService.finish); orderService.update(o); Product p = productService.get(pid); content = HtmlUtils.htmlEscape(content); User user = (User) session.getAttribute("user"); Review review = new Review(); review.setContent(content); review.setProductId(pid); review.setCreateDate(new Date()); review.setUserId(user.getId()); reviewService.add(review); return "redirect:forereview?oid=" + oid + "&showonly=true"; } ~~~ reviewPage.jsp: ~~~ <c:if test="${param.showonly==true}"> <div class="reviewDivlistReviews"> <c:forEach items="${reviews}" var="r"> <div class="reviewDivlistReviewsEach"> <div class="reviewDate"> ${r.createDate} </div> <div class="reviewContent">${r.content}</div> <div class="reviewUserInfo pull-right">${r.user.anonymousName}<span class="reviewUserInfoAnonymous">(匿名)</span> </div> </div> </c:forEach> </div> </c:if> ~~~ 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(); // 把账号里的特殊符号进行转义 // 在恶意注册的时候,会使用诸如 <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"; } @RequestMapping("forereview") public String review(Model model, int oid) { Order o = orderService.get(oid); orderItemService.fill(o); Product p = o.getOrderItems().get(0).getProduct(); List<Review> reviews = reviewService.list(p.getId()); productService.setSaleAndReviewNumber(p); model.addAttribute("p", p); model.addAttribute("o", o); model.addAttribute("reviews", reviews); return "fore/review"; } @RequestMapping("foredoreview") public String doreview(Model model, HttpSession session, @RequestParam("oid") int oid, @RequestParam("pid") int pid, String content) { Order o = orderService.get(oid); o.setStatus(OrderService.finish); orderService.update(o); Product p = productService.get(pid); content = HtmlUtils.htmlEscape(content); User user = (User) session.getAttribute("user"); Review review = new Review(); review.setContent(content); review.setProductId(pid); review.setCreateDate(new Date()); review.setUserId(user.getId()); reviewService.add(review); return "redirect:forereview?oid=" + oid + "&showonly=true"; } } ~~~ > 补充说明:日期格式化,可以在getter方法中,写格式化操作,也可以使用fmt标签格式化,学员自己完成。