⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shopcartaction.java

📁 网上购物系统用SSH实现的
💻 JAVA
字号:
package cn.com.tarena.ecport.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.tarena.ecport.biz.IProductBusiness;
import cn.com.tarena.ecport.biz.IShoppingCart;
import cn.com.tarena.ecport.biz.impl.ShoppingCartImpl;
import cn.com.tarena.ecport.pojo.Product;

public class ShopcartAction extends MappingDispatchAction {
	private IProductBusiness productbiz;
	
	public void setProductbiz(IProductBusiness productbiz) {
		this.productbiz = productbiz;
	}
	public ActionForward toShopcart(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//		ApplicationContext context = new ClassPathXmlApplicationContext("beans_hibernate.xml");
		
		HttpSession session = request.getSession();
//		从session中拿到shopcart对象,如果是第一次则新建一个。
		IShoppingCart shopcart = (IShoppingCart)session.getAttribute("shopcart");
		if(shopcart==null){
			shopcart = new ShoppingCartImpl();
		}
		if(request.getParameter("id")!=null){
			Long productid = Long.parseLong(request.getParameter("id"));
//			通过id查询对应的Product
//			IProductBusiness productbiz = (IProductBusiness)context.getBean("productBusiness");
			Product product = productbiz.getProductById(productid);
//			把查出的Product和数量传入shopcart对象,添加一条记录或在原有的记录上增加数量
			if(request.getParameter("num")!=null&&!"".equals(request.getParameter("num"))){
//				修改数量根据传进来的text框中的值
				shopcart.modifyProductAmountById(productid, Integer.parseInt(request.getParameter("num")));
			}else{
				shopcart.addProduct(product, 1);
			}
//			判断传进来的参数是否是清除类型,即查询字符串是否有cancel。
		}else if(request.getParameter("cancel")!=null){
//			如果是清空购物车则调用清空
			if(request.getParameter("cancel").equals("all")){
				shopcart.removeAllProducts();
			}else{
//			如果是id值则删除该id对应的记录
				shopcart.removeProductById(Long.parseLong(request.getParameter("cancel")));
			}
		}
//		把更新后的shopcart对象保存到session中,在shopcart.jsp中拿到当中的order迭代。
		session.setAttribute("shopcart", shopcart);
		
		return mapping.findForward("toShopcart");
	}
	
	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -