Mall project realization cart module to achieve through the session ------

1. Create a new entity class shopping cart Cart

Cart class the implements the java.io.Serializable {public 
    Private Shangpin Shangpin; // store goods entity class 
    private Integer number; // number of items 
    // setter and getter methods omitted

2. Select a product type page jump page

 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. Add Commodity object in controlelr and included 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"));
    //获取购物车
    The Map <Integer, the Cart> cartMap = (the Map <Integer, the Cart>) session.getAttribute ( "cartMap"); 
    // first add items to the shopping cart
    IF (cartMap == null) {
        cartMap = new new HashMap <Integer, cart> (); // instantiate map object
        // instantiate Object cart
        cart cart cart new new = (); cart.setShangpin (Shangpin); 
        cart.setNumber (. 1); 
        // save the goods collection object to the map
        cartMap .put (id, cart);
    } after the else {// first operation
        cart cart = cartMap.get ( "id"the product id, acquired entity classes cart
        if (! cart = null) { // presence of the same commodity   cart.setNumber (cart.getNumber () +. 1); 
        } {the else
            Cart Cart new new = ();
            cart.setShangpin (Shangpin);
            cart.setNumber (. 1);
       
          
 
            cartMap.put (ID, Cart);
        }
    } 
    // then stored in the session session.setAttribute ( "cartMap", cartMap); 
    return "Forward: getShoppingCar"; 
}
    

 4. Remove the shopping cart information from the session, and forwarded to the shopping cart page display product information

@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. Page Display

<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 = "the checked-label"> <INPUT type = "CheckBox"> <I> </ I> <TD> </ TH> </ label> <div class = "IMG">! [inserted here picture description] () </ div> <div class="name ep3" th:text="${list.value.shangpin.name}"></div> Color Classification: <div th: text = " $ {list.value.shangpin.color}"> </ div> Size: <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 = "the Add" value = "+ ">
</ div> </ TD> <TD TH: text =" $ {} list.value.shangpin.price "> </ TD>
// delete button corresponding to this id is obtained, deletes the corresponding session is specifically deleted the key to it. <td> <

  

 

Guess you like

Origin www.cnblogs.com/cainame/p/11671690.html