Hi Umesh and Paul,

I have attached the code. Cart.java is my domain class where itemlist will
have all the items added in the cart. AddItemToCartAction will add to
itemList and UpdateCartQuantitiesAction will fetch it from itemList. I
agree to what Paul ..but can you suggest a workaround??

This is actually the JPetstore project I am working on. I am rewritting the
code in Struts 2 . I select FISH,Add to Cart and then Update Cart. In
Update Cart, it shows Cart is Empty.

Please help with a solution.


On Sun, Sep 1, 2013 at 10:14 PM, Paul Benedict <pbened...@apache.org> wrote:

> Every Action class is instantiated per request. If you're updating a
> collection in one action, it will, of course, not be around in the next
> collection. It sounds like you need to stuff your shopping cart into the
> session.
>
>
> On Sun, Sep 1, 2013 at 8:39 PM, Srineel Mazumdar <smaz19...@gmail.com
> >wrote:
>
> > Hi All,
> >
> > Can anyone please help ?
> >
> > Thanks..
> >
> >
> > On Fri, Aug 30, 2013 at 10:23 PM, Srineel Mazumdar <smaz19...@gmail.com
> > >wrote:
> >
> > > Yes.. the same collection.
> > >
> > >
> > > On Wed, Aug 28, 2013 at 6:55 PM, Paul Benedict <pbened...@apache.org
> > >wrote:
> > >
> > >> Are you retrieving the same collection?
> > >>
> > >>
> > >> On Wed, Aug 28, 2013 at 8:22 AM, Srineel Mazumdar <
> smaz19...@gmail.com
> > >> >wrote:
> > >>
> > >> > Hi,
> > >> >
> > >> > I am creating a shopping cart application in Struts 2. I am
> updating a
> > >> list
> > >> > in Cart domain object when the user selects some items in shopping
> > list
> > >> > using checkboxes and clicks Next. In the CheckOut page those items
> > >> would be
> > >> > shown. But in my case it shows empty list. Upon debugging, I find
> that
> > >> list
> > >> > is updated finally, but an empty list is fetched in Check Out page.
> > >> >
> > >> > Can somebody help me by giving some pointers ?
> > >> >
> > >> > Thanks.,
> > >> > Srineel
> > >> >
> > >>
> > >>
> > >>
> > >> --
> > >> Cheers,
> > >> Paul
> > >>
> > >
> > >
> >
>
>
>
> --
> Cheers,
> Paul
>
package com.home.practice.shop.domain;



import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.springframework.beans.support.PagedListHolder;

public class Cart implements Serializable {

  /* Private Fields */

  private final Map itemMap = Collections.synchronizedMap(new HashMap());
        
  private final PagedListHolder itemList = new PagedListHolder();

  /* JavaBeans Properties */

        public Cart() {
                this.itemList.setPageSize(4);
        }

        public Iterator getAllCartItems() { return 
itemList.getSource().iterator(); }
  public PagedListHolder getCartItemList() { return itemList; }
  public int getNumberOfItems() { return itemList.getSource().size(); }

  /* Public Methods */

  public boolean containsItemId(String itemId) {
    return itemMap.containsKey(itemId);
  }

  public void addItem(Item item, boolean isInStock) {
    CartItem cartItem = (CartItem) itemMap.get(item.getItemId());
    if (cartItem == null) {
      cartItem = new CartItem();
      cartItem.setItem(item);
      cartItem.setQuantity(0);
      cartItem.setInStock(isInStock);
      itemMap.put(item.getItemId(), cartItem);
      itemList.getSource().add(cartItem);
      //changes for debugging
      System.out.println("** " + itemList.getSource().size());
    }
    cartItem.incrementQuantity();
  }


  public Item removeItemById(String itemId) {
    CartItem cartItem = (CartItem) itemMap.remove(itemId);
    if (cartItem == null) {
      return null;
    }
                else {
      itemList.getSource().remove(cartItem);
      return cartItem.getItem();
    }
  }

  public void incrementQuantityByItemId(String itemId) {
    CartItem cartItem = (CartItem) itemMap.get(itemId);
    cartItem.incrementQuantity();
  }

  public void setQuantityByItemId(String itemId, int quantity) {
    CartItem cartItem = (CartItem) itemMap.get(itemId);
    cartItem.setQuantity(quantity);
  }

  public double getSubTotal() {
    double subTotal = 0;
    Iterator items = getAllCartItems();
    while (items.hasNext()) {
      CartItem cartItem = (CartItem) items.next();
      Item item = cartItem.getItem();
      double listPrice = item.getListPrice();
      int quantity = cartItem.getQuantity();
      subTotal += listPrice * quantity;
    }
    return subTotal;
  }

}
package com.home.practice.shop;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.home.practice.shop.domain.Cart;
import com.home.practice.shop.domain.CartItem;

public class UpdateCartQuantitiesAction extends BaseAction {

        private Cart cart = new Cart();
        private String workingItemId;

        /* JavaBeans Properties */

        public Cart getCart() {
                return cart;
        }

        public void setCart(Cart cart) {
                this.cart = cart;
        }

        public String getWorkingItemId() {
                return workingItemId;
        }

        public void setWorkingItemId(String workingItemId) {
                this.workingItemId = workingItemId;
        }

        public String execute() throws Exception {
                // CartActionForm cartForm = (CartActionForm) form;
                HttpServletRequest request = ServletActionContext.getRequest();
                Iterator cartItems = getCart().getAllCartItems();
                while (cartItems.hasNext()) {
                        CartItem cartItem = (CartItem) cartItems.next();
                        String itemId = cartItem.getItem().getItemId();
                        try {
                                int quantity = 
Integer.parseInt(request.getParameter(itemId));
                                getCart().setQuantityByItemId(itemId, quantity);
                                if (quantity < 1) {
                                        cartItems.remove();
                                }
                        } catch (NumberFormatException e) {
                                // ignore on purpose
                        }
                }
                return "success";
        }

}
package com.home.practice.shop;

import com.home.practice.shop.domain.Cart;
import com.home.practice.shop.domain.Item;

public class AddItemToCartAction extends BaseAction {

        private String workingItemId;
        private Cart cart;

        /**
         * @return the cart
         */
        public Cart getCart() {
                return cart;
        }

        /**
         * @param cart
         *            the cart to set
         */
        public void setCart(Cart cart) {
                this.cart = cart;
        }

        /**
         * @return the workingItemId
         */
        public String getWorkingItemId() {
                return workingItemId;
        }

        /**
         * @param workingItemId
         *            the workingItemId to set
         */
        public void setWorkingItemId(String workingItemId) {
                this.workingItemId = workingItemId;
        }

        public String execute() throws Exception {

                if (cart.containsItemId(workingItemId)) {
                        cart.incrementQuantityByItemId(workingItemId);
                } else {
                        // isInStock is a "real-time" property that must be 
updated
                        // every time an item is added to the cart, even if 
other
                        // item details are cached.
                        boolean isInStock = 
getPetStore().isItemInStock(workingItemId);
                        Item item = getPetStore().getItem(workingItemId);
                        getCart().addItem(item, isInStock);
                }
                return "success";
        }

}
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to