zoukankan      html  css  js  c++  java
  • 把购买数据添加到购物车

    这里用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";
            }
  • 相关阅读:
    四.Oracle聚合函数和内外全连接
    三.Oracle常用数据类型及单行函数总结
    二.Sql语言的分类及运算符
    一.Oracle的安装与连接
    Maven环境的配置
    javaSE基础总结篇04
    javaSE基础总结篇03
    javaSE基础总结篇02
    JavaSE基础篇总结01
    表分区
  • 原文地址:https://www.cnblogs.com/liuna369-4369/p/10931026.html
Copyright © 2011-2022 走看看