コード例 #1
0
 // 从购物车中删除商品
 private boolean deleteFromCart(HttpServletRequest request, HttpServletResponse response) {
   String id = request.getParameter("id");
   Cart cart = (Cart) request.getSession().getAttribute("cart");
   Items item = idao.getItemsById(Integer.parseInt(id));
   if (cart.removeGoodsFromCart(item)) {
     return true;
   } else {
     return false;
   }
 }
コード例 #2
0
  // 添加商品进购物车的方法
  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;
    }
  }