private ShoppingBean bean=new ShoppingBean();//提交后数据都保存在beans中
private CartItemsBean cartitems=new CartItemsBean();
List<CartItemsBean> listcart;//作为购物车的集合


public String cart()
{
System.out.println("Id+---sdfsdf-----------------"+bean.getId());
System.out.println("Number+--------fsdfsd------------"+bean.getNumber());
System.out.println("Title+-------fsdf-------------"+bean.getTitle());
//判断购物车是否存在
if(listcart==null)
{
listcart=new ArrayList<CartItemsBean>();
}
//判断书籍是否在购物车中
if(listcart.contains(bean.getId()))
{
cartitems.setQuantity(cartitems.getQuantity()+1);
}
else
{
listcart.add(new CartItemsBean(bean,1));
}

return SUCCESS;
}
public class CartItemsBean implements Serializable {
private ShoppingBean bean;
private int quantity;
public CartItemsBean(){}
public CartItemsBean(ShoppingBean beanadd,int number)
{
bean=beanadd;
quantity=number;
}
get set 省略....       jsp页面
  <s:iterator value="listcart" id="list">
    <table>
    <tr>
    <td>编号</td>
    <td>商品名称</td>
    <td>单价</td>
    <td>数量</td>
    <td>金额</td>
    </tr>
    <tr>
    <td><s:property alue="#list.bean.number"/></td>
    <td><s:property value="#list.bean.title"/></td>
    <td><s:property value="#list.bean.price"/></td>
    <td><s:property value="quantity"/></td>
    <td><s:property value="#list.bean.price"/>*<s:property value="quantity"/>元</td>
    </tr>
    </table>
    </s:iterator>
Action中有问题!!  请各位高手一起帮帮忙啊!! 小弟感谢了!!

解决方案 »

  1.   

    //判断书籍是否在购物车中
    if(listcart.contains(bean.getId()))
    这个判断有问题么?listcart 里面存在的都是实体类,你这是不是在问它是否包含ID?假如ID=1 
    那将是这样:if(listcart.contains(3)),看看吧,我就感觉这有点问题。
      

  2.   

    listcart这个可能是空的引起的吧
    还有就是可能判断出错了
      

  3.   

      小弟只知道是Action中出了问题  但是.....
         各位大虾能提出个解决方案麽??!
      

  4.   

    //判断购物车是否存在
    if(listcart==null)
    {
    listcart=new ArrayList<CartItemsBean>();
    }
    每次执行这里 都被重新创建了  要怎么改?? 指教 指教!!
      

  5.   

    //判断书籍是否在购物车中
    if(listcart.contains(bean.getId()))
    {
    cartitems.setQuantity(cartitems.getQuantity()+1);
    }你这里的判断好像也错了。
    listcart 存的是CartItemsBean这个对象,但是你判断是否存在却用的bean.getId()这样肯定是不存在的。
      

  6.   

    购物车应该放在Session或者Cookies里面管理
      

  7.   

    每次的request都会构建一个新的aciton实例,所以listcart 每次都是null 。
    可以将listcart放入用户session中,从session 获取listcart 
      

  8.   

    List<CartItemsBean> listcart;//作为购物车的集合
    改为从seesion里面取。
      

  9.   

    购物车放session、cookie就算是直接放数据库里也行!只不过各有利弊!给你个例子:
    HttpSession session =request.getSession();
    session.setAttribute("listuser",user);
    你用hibernate了吧?
      

  10.   

    public class CartItemsAction extends ActionSupport{
    private ShoppingBean bean=new ShoppingBean();//提交后数据都保存在beans中
    private CartItemsBean cartitems=new CartItemsBean();
    private HttpServletRequest request;
    public String cart()
    {
    HttpSession session=request.getSession(false);
    //从购物车中取出书籍
    List<CartItemsBean> listcart=(List<CartItemsBean>)session.getAttribute("listcart");//作为购物车的集合
    if(listcart==null)
    {
    listcart=new ArrayList<CartItemsBean>();
    session.setAttribute("listcart", listcart);
    }

    ..................
    return SUCCESS;
    }是这样么?  那如何判断购物车中是否存在该书籍??
      

  11.   

    判断是否存在书籍.
    如果不想用循环比较的话,你可以重写CartItemsBean的equals方法,根据Id判断2个对象是否相等
    然后调用的list的indexOf(Object o)方法,获取该对象的索引,判断list中是否存在该本书
      

  12.   

    class CartItemsBean{
           @Override
    public boolean equals(Object obj) {
    if(this == obj){
    return true;
    }
    if(obj == null){
    return false;
    }
    if(!(obj instanceof CartItemsBean)){
    return false;
    }
    CartItemsBean bean = (CartItemsBean)obj;
    return (this.getId() != null && bean.getId() != null 
    && this.getId().equals(bean.getId())) ||
    (this.getId() == bean.getId());
    }
    }if(listcart.index(cartitems) != -1) {
         // 已存在
    }
      

  13.   

    想看有没有值,取session里东西输出下看看是什么:
    List<CartItemsBean> cartBean=(List<CartItemsBean>) session.getAttribute("listcart");
      

  14.   

     
    cy_gogo  的方法蛮好的,一般都那样做的  就是你在action中写的就从session中去取在判断是否存在时
    就现在实体类中重写一下equals方法  连代码都写给你了然后只要在action中调用该方法进行判断就可以了
      

  15.   

    get/set方法略//书籍信息Bean
    public class TbShopInfoEntity implements java.io.Serializable { // Fields private Integer id; private String shopName; private Float price;
    }
    /**
     * 商品项,多数量
     */
    public class ShopItem {
    private TbShopInfoEntity shop; private int quantity; public ShopItem() {
    super();
    } // 第一次购买这本书
    // 构造函数,用于书籍信息的初始化,并把书籍数量初始化为1
    public ShopItem(TbShopInfoEntity t) {
    this.shop = t;
    this.quantity = 1;
    } // 重复购买同一本书
    public void addQuantity() {
    ++quantity;
    } public void subQuantity() {
    --quantity;
    }
    }/**
     * 购物车的具体操作类 还可以保存订购书籍信息(多种类)
     */
    public class ShopCarUtil { private int total; private float totalPrice; /**
     * 保存书籍 map key :bookId map value : BookItem对象 例如:map.put(bookid, new
     * BookItem());
     */
    private Map map = new HashMap(); /**
     * 添加书籍信息
     * 用于第一次和重复订购时使用
     */
    public void addBook(TbShopInfoEntity shop) {
    // 通过书籍ID从map中查找是否有相同的书籍
    ShopItem item = (ShopItem) map.get(shop.getId());
    if (item == null) {
    // 第一次购买
    map.put(shop.getId(), new ShopItem(shop));
    } else {
    // 重复购买
    item.addQuantity();
    }
    this.total++;
    } /**
     * 清空购物车
     */
    public void remAll() {
    if (map != null) {
    map.clear();
    }
    this.total = 0;
    } /**
     * 移除一件商品
     * @param id
     */
    public void rem(Integer id) {
    ShopItem item = (ShopItem) this.map.get(id);
    if (item != null) {
    map.remove(id);
    this.total -= item.getQuantity();
    }
    }

    /**
     * 重复添加商品
     * @param id
     */
    public void add(Integer id) {
    ShopItem item = (ShopItem) this.map.get(id);
    if (item != null) {
    item.addQuantity();
    this.total++;
    }
    }

    /**
     * 修改数量
     * @param id
     * @param quty
     */
    public void update(Integer id, int quty) {
    ShopItem item = (ShopItem) this.getMap().get(id);
    if(item != null) {
    int temp = quty - item.getQuantity();
    item.setQuantity(quty);
    total += temp;
    }
    } /**
     * 减少商品数量
     * @param id
     */
    public void sub(Integer id) {
    ShopItem item = (ShopItem) this.map.get(id);
    if (item.getQuantity() == 1) {
    this.rem(id);
    } else {
    item.subQuantity();
    this.total--;
    }
    }
    /**
     * 计算商品总价
     * @return
     */
    public float getTotalPrice() {
    float p = 0.0f;
    Iterator it = this.getMap().values().iterator();
    ShopItem item = null;
    while (it.hasNext()) {
    item = (ShopItem) it.next();
    p += item.getQuantity() * item.getShop().getPrice();
    }
    return p;
    }
    }
    /**
    * Servlet 你可以改成Action,改法你应该会
    */
    public class ShopCarServlet extends HttpServlet { protected void service(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    String type = req.getParameter("type");
    if ("addBook".equals(type)) { //添加商品
    Integer id = Integer.valueOf(req.getParameter("bid"));
    TbShopInfoEntity shop = new ShopInfoDAO().findById(id);
    this.makeCar(req).addBook(shop);
    } else if ("remAll".equals(type)) { //移除所有
    this.makeCar(req).remAll();
    } else if ("rem".equals(type)) { //移除一个
    Integer id = Integer.valueOf(req.getParameter("bid"));
    this.makeCar(req).rem(id);
    } else if ("add".equals(type)) { //重复添加
    Integer id = Integer.valueOf(req.getParameter("bid"));
    this.makeCar(req).add(id);
    } else if ("sub".equals(type)) { //减少商品
    Integer id = Integer.valueOf(req.getParameter("bid"));
    this.makeCar(req).sub(id);
    } else if ("update".equals(type)) { //修改商品
    Integer id = Integer.valueOf(req.getParameter("bid"));
    int quty = Integer.parseInt(req.getParameter("quty").toString());
    this.makeCar(req).update(id, quty);
    }
    // 跳转
    res.sendRedirect("xxx.jsp");
    } /*
     * 是一个session的初始化方法 1. 为程序返回ShopCarUtil 2. 为了创建session会话
     */
    public ShopCarUtil makeCar(HttpServletRequest req) {
    HttpSession session = req.getSession();
    // 先获得会话中的car对象
    ShopCarUtil shopCar = (ShopCarUtil) session.getAttribute("car");
    // 创建购物车(在第一次添加购物车时使用)
    if (shopCar == null) {
    shopCar = new ShopCarUtil();
    session.setAttribute("car", shopCar);
    return shopCar;
    } else { // 当重复使用购物车时
    return shopCar;
    }
    }
    }