1.有数据传递<%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>
2.记得导入jar包 jstl.jar /standard.jar
定义一个类ShopItem
package com.flowershop.entity;
public class ShopItem {
private int buyCount;
private Flower buyProduct ;
public int getBuyCount() {
return buyCount;
}
public void setBuyCount(int buyCount) {
this.buyCount = buyCount;
}
public Flower getBuyProduct() {
return buyProduct;
}
public void setBuyProduct(Flower buyProduct) {
this.buyProduct = buyProduct;
}
public ShopItem(int buyCount, Flower buyProduct) {
super();
this.buyCount = buyCount;
this.buyProduct = buyProduct;
}
public ShopItem( Flower buyProduct) {
this.buyCount =1;
this.buyProduct = buyProduct;
}
public ShopItem() {
super();
// TODO Auto-generated constructor stub
}
}
<c:forEach items="${mapcar}" var=“entry” varStatus=“vs”>
3.package com.flowershop.servlet;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.flowershop.dao.FlowerDao;
import com.flowershop.entity.Flower;
import com.flowershop.entity.ShopItem;
/**
-
Servlet implementation class ChangeCart
*/
@WebServlet("/ChangCarServlet")
public class ChangCarServlet extends HttpServlet {
private static final long serialVersionUID = 1L;/**
- @see HttpServlet#HttpServlet()
*/
public ChangCarServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
- @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
-
response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {//实现购物车的数量 response.setContentType("text/html;charset=utf-8"); String id =request.getParameter("id"); int idd=Integer.parseInt(id); System.out.println("id"+idd); //数量的类型转换 String count = request.getParameter("count"); int newCount = Integer.parseInt(count); HashMap<String, ShopItem> mapCar = (HashMap<String, ShopItem>) request .getSession().getAttribute("mapcar"); ShopItem item = mapCar.get(id); // 存map的值判断是否有购物车 if (newCount == 0) { mapCar.remove(id); } else { //调整商品的数量 item.setBuyCount(newCount); } if(mapCar.size()==0){ request.getSession().removeAttribute("mapcar"); } request.getSession().setAttribute("mapcar", mapCar); response.sendRedirect("index/shoppingflowercart.jsp");
}
} - @see HttpServlet#HttpServlet()