購買行為 就是創建一條一條的訂單項
而顯示購物車,也就是把這些訂單項顯示在頁面上。
在這個階段,訂單項都會保存在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>


