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");
}
}