企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 步骤 1 : 先运行,看到效果,再学习 先将完整的 tmall_ssm 项目(向老师要相关资料),配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。 ## 步骤 2 : 模仿和排错 在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。 模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较**正确答案** ( 可运行项目 ) 和自己的代码,来定位问题所在。 采用这种方式,**学习有效果,排错有效率**,可以较为明显地提升学习速度,跨过学习路上的各个槛。 ## 步骤 3 : 界面效果 ![](https://box.kancloud.cn/97356d1ce83e4c58a6cb991af5d1c6be_1288x874.png) ## 步骤 4 : ForeController.buy() 在上个知识点立即购买最后,客户端跳转到结算路径: `"@forebuy?oiid="+oiid;` `http://127.0.0.1:8080/tmall_ssm/forebuy?oiid=1` 导致ForeController.buy()方法被调用 1. 通过字符串数组获取参数oiid 为什么这里要用字符串数组试图获取多个oiid,而不是int类型仅仅获取一个oiid? 因为根据购物流程环节与表关系,结算页面还需要显示在购物车中选中的多条OrderItem数据,所以为了兼容从购物车页面跳转过来的需求,要用字符串数组获取多个oiid 2. 准备一个泛型是OrderItem的集合ois 3. 根据前面步骤获取的oiids,从数据库中取出OrderItem对象,并放入ois集合中 4. 累计这些ois的价格总数,赋值在total上 5. 把订单项集合放在session的属性 "ois" 上 6. 把总价格放在 model的属性 "total" 上 7. 服务端跳转到buy.jsp ![](https://box.kancloud.cn/4b978f833db73213cb41c8fb4c8ce787_688x501.png) ~~~ 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.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"; } } ~~~ ## 步骤 5 : buy.jsp 与**register.jsp** 相仿,buy.jsp也包含了header.jsp, top.jsp, footer.jsp 等公共页面。 中间是结算业务页面 buyPage.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" %> <%@include file="../include/fore/header.jsp"%> <%@include file="../include/fore/top.jsp"%> <%@include file="../include/fore/cart/buyPage.jsp"%> <%@include file="../include/fore/footer.jsp"%> ~~~ ## 步骤 6 : buyPage.jsp 1. 遍历出订单项集合 ois 中的订单项数据 从立即购买跳转到结算页面来只会有一件产品 2. 显示总金额 ![](https://box.kancloud.cn/2a93ffef6d49be92900c2d99612cae68_1294x365.png) ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <div class="buyPageDiv"> <form action="forecreateOrder" method="post"> <div class="buyFlow"> <img class="pull-left" src="img/site/simpleLogo.png"> <img class="pull-right" src="img/site/buyflow.png"> <div style="clear: both"></div> </div> <div class="address"> <div class="addressTip">输入收货地址</div> <div> <table class="addressTable"> <tr> <td class="firstColumn">详细地址<span class="redStar">*</span></td> <td><textarea name="address" placeholder="建议您如实填写详细收货地址,例如接到名称,门牌好吗,楼层和房间号等信息"></textarea></td> </tr> <tr> <td>邮政编码</td> <td><input name="post" placeholder="如果您不清楚邮递区号,请填写000000" type="text"></td> </tr> <tr> <td>收货人姓名<span class="redStar">*</span></td> <td><input name="receiver" placeholder="长度不超过25个字符" type="text"></td> </tr> <tr> <td>手机号码 <span class="redStar">*</span></td> <td><input name="mobile" placeholder="请输入11位手机号码" type="text"></td> </tr> </table> </div> </div> <div class="productList"> <div class="productListTip">确认订单信息</div> <table class="productListTable"> <thead> <tr> <th colspan="2" class="productListTableFirstColumn"><img class="tmallbuy" src="img/site/tmallbuy.png"> <a class="marketLink" href="#nowhere">店铺:天猫店铺</a> <a class="wangwanglink" href="#nowhere"> <span class="wangwangGif"></span> </a></th> <th>单价</th> <th>数量</th> <th>小计</th> <th>配送方式</th> </tr> <tr class="rowborder"> <td colspan="2"></td> <td></td> <td></td> <td></td> <td></td> </tr> </thead> <tbody class="productListTableTbody"> <c:forEach items="${ois}" var="oi" varStatus="st"> <tr class="orderItemTR"> <td class="orderItemFirstTD"><img class="orderItemImg" src="img/productSingle_middle/${oi.product.firstProductImage.id}.jpg"></td> <td class="orderItemProductInfo"><a href="foreproduct?pid=${oi.product.id}" class="orderItemProductLink"> ${oi.product.name} </a> <img src="img/site/creditcard.png" title="支持信用卡支付"> <img src="img/site/7day.png" title="消费者保障服务,承诺7天退货"> <img src="img/site/promise.png" title="消费者保障服务,承诺如实描述"></td> <td><span class="orderItemProductPrice">¥<fmt:formatNumber type="number" value="${oi.product.promotePrice}" minFractionDigits="2" /></span></td> <td><span class="orderItemProductNumber">${oi.number}</span></td> <td><span class="orderItemUnitSum"> ¥<fmt:formatNumber type="number" value="${oi.number*oi.product.promotePrice}" minFractionDigits="2" /> </span></td> <c:if test="${st.count==1}"> <td rowspan="5" class="orderItemLastTD"><label class="orderItemDeliveryLabel"> <input type="radio" value="" checked="checked"> 普通配送 </label> <select class="orderItemDeliverySelect" class="form-control"> <option>快递 免邮费</option> </select></td> </c:if> </tr> </c:forEach> </tbody> </table> <div class="orderItemSumDiv"> <div class="pull-left"> <span class="leaveMessageText">给卖家留言:</span> <span> <img class="leaveMessageImg" src="img/site/leaveMessage.png"> </span> <span class="leaveMessageTextareaSpan"> <textarea name="userMessage" class="leaveMessageTextarea"></textarea> <div> <span>还可以输入200个字符</span> </div> </span> </div> <span class="pull-right">店铺合计(含运费): ¥<fmt:formatNumber type="number" value="${total}" minFractionDigits="2" /></span> </div> </div> <div class="orderItemTotalSumDiv"> <div class="pull-right"> <span>实付款:</span> <span class="orderItemTotalSumSpan">¥<fmt:formatNumber type="number" value="${total}" minFractionDigits="2" /></span> </div> </div> <div class="submitOrderDiv"> <button type="submit" class="submitOrderButton">提交订单</button> </div> </form> </div> ~~~