SSM购物车之购物车模块day04
购买行为 就是创建一条一条的订单项
而显示购物车,也就是把这些订单项显示在页面上。
在这个阶段,订单项都会保存在session中,直到最后生成订单的时候,才会把这些订单项保存在数据库中。
1.SQL
暂时不需要为OrderItem创建表,因为在这个环节OrderItem还是保存在Session中的
2.实体类
1 package cn.gb.pojo; 2 3 public class OrderItem { 4 private String id; 5 private Product product; 6 private Order order; 7 private int num; 8 9 public Order getOrder() { 10 return order; 11 } 12 13 public void setOrder(Order order) { 14 this.order = order; 15 } 16 17 public String getId() { 18 return id; 19 } 20 21 public void setId(String id) { 22 this.id = id; 23 } 24 25 public Product getProduct() { 26 return product; 27 } 28 29 public void setProduct(Product product) { 30 this.product = product; 31 } 32 33 public int getNum() { 34 return num; 35 } 36 37 public void setNum(int num) { 38 this.num = num; 39 } 40 }
3.Mapper层(dao)
4.Service(业务层)
5.Controller(控制层)
1. 获取购买数量
2. 获取购买商品的id
3. 根据id获取商品对象
4. 创建一个新的OrderItem对象
5. 从session中取出一个List , 这个List里面存放陆续购买的商品。
如果是第一次从session中获取该List,那么它会是空的,需要创建一个ArrayList
6. 把新创建的OrderItem对象放入该List 中
7. 跳转到显示购物车的listOrderItem
当加入相同的商品时-----------
遍历session中所有的OrderItem
如果找到对应的product.id一样的条目,就调整其数量
如果没有找到,就新增加一条
1 package cn.gb.controller; 2 3 import cn.gb.pojo.OrderItem; 4 import cn.gb.pojo.Product; 5 import cn.gb.service.ProductService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.http.HttpRequest; 8 import org.springframework.stereotype.Controller; 9 import org.springframework.ui.Model; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.ResponseBody; 12 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpSession; 15 import java.util.ArrayList; 16 import java.util.List; 17 18 @Controller 19 @RequestMapping("cart") 20 public class CartController { 21 @Autowired 22 private ProductService productService; 23 24 @RequestMapping("showCart") 25 public String showCart( HttpSession session, Model model){ 26 List<OrderItem> ois= (List<OrderItem>)session.getAttribute("carts"); 27 if (null!=ois){ 28 model.addAttribute("ois",ois); 29 } 30 return "/cart.jsp"; 31 } 32 33 @RequestMapping("addCart") 34 @ResponseBody 35 public String addCart(String pid,String num,HttpSession session){ 36 OrderItem oi=new OrderItem(); 37 Product product=productService.info(pid); 38 oi.setNum(Integer.parseInt(num)); 39 oi.setProduct(product); 40 List<OrderItem> itemList= (List<OrderItem>) session.getAttribute("carts"); 41 if (null==itemList){ 42 itemList=new ArrayList<>(); 43 session.setAttribute("carts",itemList); 44 } 45 boolean found=false; 46 for (OrderItem orderItem : itemList){ 47 if (orderItem.getProduct().getId()==oi.getProduct().getId()){ 48 orderItem.setNum(orderItem.getNum()+oi.getNum()); 49 found=true; 50 break; 51 } 52 } 53 if (!found) 54 itemList.add(oi); 55 return "true"; 56 } 57 }
6.jsp页面
加入
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2021/2/2 0002
Time: 10:24
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
<script src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js"/>
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css">
</head>
<script>
$(function () {
$(".addBtn").click(function () {
var button=$(this);
var pid=$(this).attr("pid");
/* alert(pid);*/
var number=$(".number[pid="+pid+"]").val();
/*alert(number);*/
$.ajax({
url:"${pageContext.request.contextPath}/cart/addCart.action",
data:{"pid":pid,"num":number},
success:function (result) {
window.location.href="/cart/showCart.action"
}
})
})
})
</script>
<body>
<jsp:include page="header.jsp"/>
<table class="table table-striped table-bordered"
style="width: 500px;margin: 44px auto" border="1" align="center" cellspacing="0">
<tr>
<td>编号</td>
<td>名称</td>
<td>价格</td>
<td>购买</td>
</tr>
<c:forEach items="${list}" var="pro">
<tr>
<td>${pro.id}</td>
<td>${pro.name}</td>
<td>${pro.price}</td>
<td>
数量<input pid="${pro.id}" type="text" class="number" value="1" name="num">
<input type="submit" pid="${pro.id}" class="addBtn" value="购买">
</td>
</tr>
</c:forEach>
</table>
<jsp:include page="footer.jsp"/>
</body>
</html>
展示
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2021/2/3 0003
Time: 11:15
To change this template use File | Settings | File Templates.
--%>
<%@ 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" %>
<html>
<head>
<title>Title</title>
<script src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js"/>
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css">
</head>
<form id="orderForm"
style="margin-top: 5px; margin-left: 150px;"
action="${pageContext.request.contextPath}/order/saveOrder.action" method="post">
<table class="table table-striped table-bordered"
style="width: 400px;margin: 44px auto" border="1" align="center" cellspacing="0">
<tr>
<td>商品名称</td>
<td>单价</td>
<td>数量</td>
<td>小计</td>
</tr>
<c:forEach var="oi" items="${ois}">
<tr>
<td>${oi.product.name}</td>
<td>${oi.product.price}</td>
<td>${oi.num}</td>
<td><fmt:formatNumber minFractionDigits="1" value="${oi.product.price*oi.num}"></fmt:formatNumber></td>
</tr>
</c:forEach>
<c:if test="${!empty ois}">
<tr>
<td colspan="5" align="right">
<a href="javascript:document.getElementById('orderForm').submit();">
<img src="${pageContext.request.contextPath}/image/finalbutton.gif" width="204" height="51"
border="0" />
</a>
</td>
</tr>
</c:if>
</table>
</form>
<nav>
</nav>
</body>
</html>


浙公网安备 33010602011771号