// 添加商品进购物车 private Boolean addToCart(HttpServletRequest request, HttpServletResponse response) { String id = request.getParameter("id"); String number = request.getParameter("num"); // 如果是第一次添加商品进购物车,必须先创建购物车设置session变量 if (request.getSession().getAttribute("cart") == null) { Cart cart = new Cart(); request.getSession().setAttribute("cart", cart); } Cart cart = (Cart) request.getSession().getAttribute("cart"); Item item = iDao.getItemById(Integer.parseInt(id)); if (cart.addGoodsInCart(item, Integer.parseInt(number))) { return true; } else { return false; } }
// 添加商品进购物车的方法 private boolean addToCart(HttpServletRequest request, HttpServletResponse response) { String id = request.getParameter("id"); String number = request.getParameter("num"); Items item = idao.getItemsById(Integer.parseInt(id)); // 是否是第一次给购物车添加商品,需要给session中创建一个新的购物车对象 if (request.getSession().getAttribute("cart") == null) { Cart cart = new Cart(); request.getSession().setAttribute("cart", cart); } Cart cart = (Cart) request.getSession().getAttribute("cart"); if (cart.addGoodsInCart(item, Integer.parseInt(number))) { return true; } else { return false; } }