温故而知新。工程结构:
public class Product { private int id; private String name; private double price; public Product(int id, String name, double price) { this.id = id; this.name = name; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Product{" + "id=" + id + ", name='" + name + '\'' + ", price=" + price + '}'; } }
import com.yz.bean.Product; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.util.*; /** * @description: * @author: yz * @create: 2018/11/7 11:06 */ @Controller public class ProductController { private static List<Product> list = new ArrayList<Product>(); // 只会执行一次,当类的字节码被加载到jvm的时候,只会执行一次 static { list.add(new Product(1,"小米5",1999)); //1 list.add(new Product(2,"海尔冰箱",2999)); //2 list.add(new Product(3,"vivo8",3999)); //3 list.add(new Product(4,"iphon9",6999)); list.add(new Product(5,"联想鼠标垫",50)); } // 显示商品列表 @RequestMapping("listProduct") public String listProduct(Model model){ // 准备商品 // 存商品 model.addAttribute("list",list); return "list"; } // 添加商品到购物车中,需要商品的编号 @RequestMapping("addToCart") public String addToCart(HttpServletRequest request , HttpServletResponse response, int id){ System.out.println("id:"+id); // 获取sessoin HttpSession session = request.getSession(); // 获取商品对象 Product product = list.get(id - 1); // 存储到购物车 Map<Product , Integer> cartMap = (Map<Product, Integer>) session.getAttribute("cartMap"); if(cartMap == null){ cartMap = new HashMap<Product , Integer>(); } if(cartMap.containsKey(product)){ // 有这件商品,原来的数量+1 cartMap.put(product, cartMap.get(product)+1); }else { cartMap.put(product,1); } // 把购物车存到session中去 session.setAttribute("cartMap",cartMap); System.out.println("添加购物车完毕,要跳转到中转页面了~"); // 为了让购物车存的时间更长久一点,现在需要手动设置cookie Cookie cookie = new Cookie("JSESSIONID" , session.getId()); cookie.setMaxAge(60*60*24*7); response.addCookie(cookie); //4. 跳转页面 当前的请求是从模板页面发送出来的,所以这里也认为是跑到模板页面去了。 return "redirect:transfer.html"; } //跳转到购物车的页面 @RequestMapping("toCart") public String toCart(){ return "cart"; } }
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @description: * @author: yz * @create: 2018/11/7 11:03 */ @SpringBootApplication public class CartApp { public static void main(String [] args){ SpringApplication.run(CartApp.class,args); } }
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2><a href="listProduct">开始购物</a></h2> </body> </html>
transfer.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <!--继续回到商品列表的方法去u--> <h2><a href="listProduct">继续购物</a></h2><br><br> <h2><a href="toCart">去购物车结算</a></h2><br><br> </body> </html>
cart.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" > <head> <meta charset="UTF-8"/> <title>购物车</title> </head> <body> <table> <thead> <td width="100px">编号</td> <td width="100px">名称</td> <td width="100px">价格</td> <td width="100px">数量</td> <td width="100px">小计</td> </thead> <tbody> <tr th:each="entry:${session.cartMap}"> <td th:text="${entry.key.id}">1</td> <td th:text="${entry.key.name}">2</td> <td th:text="${entry.key.price}">3</td> <td th:text="${entry.value}">3</td> <td th:text="${entry.key.price * entry.value}">4</td> </tr> </tbody> </table> </body> </html>
list.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" > <head> <meta charset="UTF-8"/> <title>Title</title> </head> <body> <h2>商品列表</h2> <table> <thead> <td width="100px">编号</td> <td width="100px">名称</td> <td width="100px">价格</td> <td width="100px">操作</td> </thead> <tbody> <tr th:each="p:${list}"> <td th:text="${p.id}">1</td> <td th:text="${p.name}">小米99</td> <td th:text="${p.price}">1999</td> <td><a th:href="@{addToCart(id=${p.id})}">加入购物车</a></td> <!--<td><a th:href="@{addToCart(id=${p.id},name=${p.name})}">加入购物车</a></td>--> <!--th:href="@{/product/comments(prodId=${prod.id})}" addToCart?id=1&name=aaa&age=123123 th:href="@{addToCart(id=${p.id})}"--> </tr> </tbody> </table> </body> </html>
build.gradle
plugins { id 'java' } group 'com.yz' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' compile("org.springframework.boot:spring-boot-starter-web:1.5.10.RELEASE") compile("org.thymeleaf:thymeleaf-spring4:2.1.4.RELEASE") }
效果: