这里用map存放购买信息,键值对为 产品信息,和购买数量
@RequestMapping("/addCart.do") public String addCart(Product product,HttpSession session,Model model){ product=productService.findProductById(product.getId()); Map<Product,Integer> carts= (Map<Product, Integer>) session.getAttribute("carts"); if(carts==null){ carts= new HashMap<Product,Integer>(); } Integer count = (Integer)carts.get(product); if(count==null){ carts.put(product, new Integer(1)); }else if(count==product.getPnum()){ model.addAttribute("fail","购买商品达到上限,无法继续购买!"); return "/client/cart.jsp"; }else{ carts.put(product,count+1); } session.setAttribute("carts",carts); return "/client/cart.jsp"; } @RequestMapping("/changeCart.do") public String changeCart(Integer id,Integer count,HttpSession session){ Product product=productService.selectProductById(id); Map<Product,Integer> carts= (Map<Product, Integer>) session.getAttribute("carts"); if(count==0){ carts.remove(product); }else{ carts.put(product,count); } return "/client/cart.jsp"; }