日期:2014-05-19  浏览次数:20886 次

购物车不能将相同的产品数量往上加啊,贴代码求高手帮忙解决,急
CartAction.java

package com.baba.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;

import com.baba.action.shopping.BaseItems;
import com.baba.action.shopping.BuyCart;
import com.baba.action.shopping.SiteSessionListener;
import com.baba.action.shopping.WebUtil;
import com.baba.model.ProductInfo;
import com.baba.model.ProductStyle;
import com.baba.service.ProductManager;
import com.baba.vo.CartFrom;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings({ "serial", "unchecked" })
@Component("carAction")
public class CartAction extends ActionSupport implements ModelDriven{

private BuyCart buyCart;
private CartFrom cf = new CartFrom();
private ProductManager pm;

public String exec() {
buyCart = (BuyCart)ServletActionContext.getRequest().getSession().getAttribute("buycart");//获取session中的购物车
if(buyCart==null) {//如果是已经打开的IE,从中拿购物车
System.out.println("-----1");
//String sid = WebUtil.getCookieByName(ServletActionContext.getRequest(), "sid");
String sid = WebUtil.getCookieByName(ServletActionContext.getRequest(), "buycartid");//从cookie中拿sid
if(sid!=null) {
System.out.println("sid="+sid);
HttpSession session = SiteSessionListener.getSession(sid);//根据sid拿到对应的session(主要是防止用户新开的IE中创建了一个新的session)
if(session!=null) {
System.out.println("有无执行啊?");
buyCart = (BuyCart) session.getAttribute("buycart");//获取购物车
}
}
}
if(buyCart==null) {//如果是刚打开IE是一个新的session,给它创建购物车和设置cookie
System.out.println("-----2");
HttpSession session = ServletActionContext.getRequest().getSession();
System.out.println("creat="+session.getId());
buyCart = new BuyCart();
session.setAttribute("buycart",buyCart);
}
if(cf.getProductid()>0) {
ProductInfo product = pm.find(ProductInfo.class, cf.getProductid());//根据接收到的产品ID找到对应的产品
if(product!=null) {
/*ProductStyle currentStyle = null;//当前样式
for(ProductStyle style:product.getStyles()){//循环当前产品样式,因为在购物期间有可能购买一个产品有多种样式
if(style.isVisible() && style.equals(new ProductStyle(cf.getStyleid()))) {//
currentStyle = style;//设置当前样式
}
}
product.getStyles().clear();//清楚产品的所有样式
if(currentStyle!=null) {
product.addProductStyle(currentStyle);//把产品的样式设置成接收的回来的ID值(真正做到 一个产品同一样式的购物项)
}
System.out.println(product.getStyles().size());*/
BaseItems bi = new BaseItems(product,1);
buyCart.addBaseItems(bi);//把产品放入购物车中
}
}

return "exec";
}

public BuyCart getBuyCart() {
return buyCart;
}

public CartFrom getCf() {
return cf;
}
public Object getModel() {
return cf;
}

public ProductManager getPm() {
return pm;
}

public void setBuyCart(BuyCart buyCart) {
this.buyCart = buyCart;
}

public void setCf(CartFrom cf) {
this.cf = cf;
}

@Resource(name="productManager")
public void setPm(ProductManager pm) {
this.pm = pm;
}
}

监听器类
package com.baba.action.shopping;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.stru