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

J2EE不能获取字段值
model类
Java code
@SuppressWarnings("serial")
@Entity
public class ShoppingCart implements Serializable{
    private int id;
    private ProductInfo product;
    private ProductStyle style;
    private String user;
    private int count=1;
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        return result;
    }
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
    public ShoppingCart(){}
    public ShoppingCart(ProductInfo product, ProductStyle style) {
        super();
        this.product = product;
        this.style = style;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ShoppingCart other = (ShoppingCart) obj;
        if (id != other.id)
            return false;
        return true;
    }
    @Id@GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @ManyToOne(cascade=CascadeType.REFRESH,optional=false)
    @JoinColumn(name="productid")
    public ProductInfo getProduct() {
        return product;
    }
    public void setProduct(ProductInfo product) {
        this.product = product;
    }
    @ManyToOne(cascade=CascadeType.REFRESH,optional=false)
    @JoinColumn(name="styleid")
    public ProductStyle getStyle() {
        return style;
    }
    public void setStyle(ProductStyle style) {
        this.style = style;
    }
    public String getUser() {
        return user;
    }
    public void setUser(String user) {
        this.user = user;
    }
}



Action类
Java code
public class CartAction extends ActionSupport implements ModelDriven{
    
    private CartFrom cf = new CartFrom();
    private ProductManager pm;
    private List<ShoppingCart> items = new ArrayList<ShoppingCart>();
    
    public String exec() {
        StringBuffer hql = new StringBuffer();
        //where product.id=cf.getid and style.id=cf.getid
        hql.append("o.product.id=? and o.style.id=? and o.user=?");
        Object[] param = new Object[]{cf.getProductid(),cf.getStyleid(),cf.getUser()};
        QueryResult<ShoppingCart> qr = pm.getScrollData(ShoppingCart.class,-1,-1, hql.toString(), param);
        if (qr.getGetResult().size()>0) {
            ShoppingCart car = (ShoppingCart) qr.getGetResult().get(0);
            car.setCount(car.getCount()+1);
            pm.update(car);
        } else {
            ShoppingCart cart = new ShoppingCart(new ProductInfo(cf.getProductid()),new ProductStyle(cf.getStyleid()));
            cart.setUser(cf.getUser());
            pm.save(cart);
            pm.refresh(cart);
        }
        items =  pm.getScrollData(ShoppingCart.class,-1,-1,"o.user=?", new Object[]{cf.getUser()}).getGetResult();
        for(ShoppingCart sc : items) {
            System.out.println(sc.getProduct().getName());
            System.out.println(sc.getCount());
        }
        return "exec";
    }

    public List<ShoppingCart> getItems() {
        return items;
    }

    public void setItems(List<ShoppingCart> items) {
        this.items = items;
    }

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

    public ProductManager getPm() {
        return pm;
    }


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

    @Resource(name="