实现购物车,及数量的加减

com.bookstore.enity

package com.bookstore.enity;

public class ShopItem {
private Product buyProduct;
private int buyCount;

public ShopItem(Product buyProduct) {
	this.buyProduct = buyProduct;
	this.buyCount = 1;
}
public Product getBuyProduct() {
	return buyProduct;
}
public void setBuyProduct(Product buyProduct) {
	this.buyProduct = buyProduct;
}
public int getBuyCount() {
	return buyCount;
}
public void setBuyCount(int buyCount) {
	this.buyCount = buyCount;
}

}

jsp
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>

购物车

购物中空空如也

序号商品名称价格数量库存小计取消
${vs.index+1}${entry.value.buyProduct.name}${entry.value.buyProduct.price}

<!-实现购物车,及数量的加减-><!-实现购物车,及数量的加减->





${entry.value.buyProduct.pnum}

${entry.value.buyProduct.price * entry.value.buyCount}


X


</c:forEach>

</c:if>

com.bookstore.servlet
package com.bookstore.client;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bookstore.enity.ShopItem;

/**

  • Servlet implementation class ChangCarServlet
    */
    @WebServlet("/ChangCarServlet")
    public class ChangCarServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public ChangCarServlet() {
    super();
    // TODO Auto-generated constructor stub
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //=实现 购物车中商品数量的调整======
    String id =request.getParameter(“id”);
    String count =request.getParameter(“count”);
    int nowCount=Integer.parseInt(count);
    //从session中取购物车
    HashMap<String ,ShopItem> mapCar=(HashMap<String , ShopItem>)request.getSession().getAttribute(“mapcar”);
    ShopItem item=mapCar.get(id);

       if(nowCount==0){ //移除商品
     	  mapCar.remove(id);
       }
       else{  //调整商品数量
           item.setBuyCount(nowCount );
       }
       if(mapCar.size()==0){  //删除购物车
     	  request.getSession().removeAttribute("mapcar");
       }
       request.getSession().setAttribute("mapcar", mapCar);
       
       response.sendRedirect("client/showCar.jsp");
    

    }
    }

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值