zoukankan      html  css  js  c++  java
  • 商城项目的购物车模块的实现------通过session实现

    1.新建购物车的实体类Cart

    public class Cart implements java.io.Serializable{
        private Shangpin shangpin;//存放商品实体类的
        private Integer number;//商品数量
        //setter和getter方法省略
    

    2.在选择商品类型页面进行页面跳转

     function shoppingCar() {
                var id=$("#baga").val();
                var color=$("#hiddenColor").val();
                var size=$("#hiddenSize").val();
                 location.href="/demo/shoppingCart?id="+id+"&color="+color+"&size="+size;
            }
    

    3.在controlelr进行添加商品对象并计入session

    @RequestMapping("/shoppingCart")
        public String shoppingCart(Integer id,HttpSession session,String color,String size){
        Integer ids=Integer.valueOf(id);
        //根据id获取商品对象
        List<Map> list=selectService.spMessage(ids);
        Shangpin shangpin=new Shangpin();
        shangpin.setPicpath((String) list.get(0).get("picpath"));
        shangpin.setColor(color);
        shangpin.setSize(size);
        shangpin.setName((String)list.get(0).get("name"));
        shangpin.setPrice((Double) list.get(0).get("price"));
        shangpin.setId((Integer) list.get(0).get("id"));
        //获取购物车
        Map<Integer, Cart> cartMap=(Map<Integer, Cart>)session.getAttribute("cartMap");
        //第一次添加商品到购物车
        if(cartMap==null){
            cartMap=new HashMap<Integer, Cart>();//实例化map对象
            //实例化购物车对象
            Cart cart=new Cart();
            cart.setShangpin(shangpin);
            cart.setNumber(1);
            //保存商品对象到map集合中
            cartMap.put(id,cart);
        }else{//第一次以后的操作
            Cart cart=cartMap.get("id");//根据商品id,获取购物车实体类
            if(cart!=null){//存在相同的商品
                cart.setNumber(cart.getNumber()+1);
    
            }else{
                cart=new Cart();
                cart.setShangpin(shangpin);
                cart.setNumber(1);
                cartMap.put(id,cart);
            }
        }
        //然后保存到session中
        session.setAttribute("cartMap",cartMap);
        return "forward:getShoppingCar";
    }
    

     4.从session中取出购物车信息,并转发到购物车页面展示商品信息

    @RequestMapping("getShoppingCar")
        public String getShoppingCar(HttpSession session,Model model){
        Map<Integer,Cart> cartMap =(Map<Integer,Cart>)session.getAttribute("cartMap");
        model.addAttribute("carList",cartMap);
        return "udai_shopcart";
    }
    

    5.页面显示

    <table class="table table-bordere">
    <thead>
    <tr> <th width="150"> <label class="checked-label"><input type="checkbox" class="check-all"><i></i> 全选</label> </th> <th width="300">商品信息</th> <th width="150">单价</th> <th width="200">数量</th> <th width="200">现价</th> <th width="80">操作</th>
    </tr> </thead> <tbody> <tr th:each="list:${carList}"> <th scope="row"> <label class="checked-label"><input type="checkbox"><i></i> <div class="img">![在这里插入图片描述]()</div> </label> </th> <td> <div class="name ep3" th:text="${list.value.shangpin.name}"></div> 颜色分类:<div th:text="${list.value.shangpin.color}"></div>尺码:<div th:text="${list.value.shangpin.size}"></div> </td> <td th:text="${list.value.shangpin.price}">¥20.0</td> <td> <div class="cart-num__box"> <input type="button" class="sub" value="-"> <input type="text" class="val" value="1" maxlength="2"> <input type="button" class="add" value="+">
    </div> </td> <td th:text="${list.value.shangpin.price}"></td>
    //这里删除按钮可获得对应的id,具体删除就是删除对应session中的键值对就可以了。 <td><a th:onclick="caonima([[${list.value.shangpin.id}]])">删除</a></td> </tr> </tbody> </table>

      

     

  • 相关阅读:
    OM Shipping 授权
    Account Summary 汇总模板
    OM Record is currently being worked on by another user, Please try to update it later.
    Initialization SQL Statement – Custom 配置错误,导致无法加载FORM
    ReOpen a Closed Inventory Accounting Period [ID 472631.1]
    IBM AIX User Lists
    Killing Oracle Sessions
    APPFND01016 (GL > Setup > Flexfields > Key > Rules)
    OM: release hold的时候,又遇到限制:You are not authorized to release this hold.
    SO History
  • 原文地址:https://www.cnblogs.com/cainame/p/11671690.html
Copyright © 2011-2022 走看看