input数量的加减

该博客介绍了如何在Java Web应用中使用JSTL标签库处理数据,特别是通过`<c:forEach>`迭代Map对象。文章提到了需要导入jstl.jar和standard.jar,并展示了如何在Servlet中实现数据操作的相关代码。

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

    }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值