Search code examples
javaejbjavabeansfaceletsenterprise

Stateful Session Bean updates(?) on buttonclick


I'm fairly new to Java EE and I'm building a simple webshop using maven web application . I have a problem with my Stateful Session Bean. I've been searching the net and tried different sollutions(most of them for using servlets) but it doesn't seem to work.

Anyway, my problem is that I'm trying to use my session bean to keep track of what's in the shopping cart. I am using an arrayList to store the items. However, when I add a new item it just replaces the old item instead of adding it to the list. I'm guessing the session bean somehow updates or a new instance of it is created but I just can't seem to find any sollution or information about this.

The stateful session bean

@Stateful
@LocalBean
public class CartSessionBean{    
    private List contents;

    public CartSessionBean(){        
        contents= new ArrayList();     
    }

    public List getContents() {
        return contents; 
    }

    public void addProduct(String title) {
        contents.add(title);   
    }
 }

The Managed Bean

@ManagedBean 
@RequestScoped
public class ProductController {

    private List cartList = new ArrayList();    
    private int nrOfCartItems=0;
    @EJB private CartSessionBean cart;  


    public String doAddCart(String title)
    {
        cart.addProduct(title);          
        setCartList(cart.getContents());         
        setNrOfCartItems(cart.getContents().size());  
        return "products.xhtml";
    }
 }

The Facelet

<h:form>                        
<p>                            
    your cart contains <h:outputLabel class="" value="#{productController.nrOfCartItems}" /> items.                           
    <ui:repeat value="#{productController.cartList}" var="cart">                                        
        <h:outputLabel value="#{cart}" />                              
    </ui:repeat>                            
    <h:commandButton value="go to checkout"/>                   
</p>                            
</h:form>

<h:form>                    
<h:dataTable value="#{productController.productList}" var="pr" border="0">                        
    <h:column>                            
        <h:graphicImage value="images/#{pr.picture}" />                        
    </h:column>                        
    <h:column>                            
        <h2><h:outputText value="#{pr.product_name}"/></h2>                             
        <p> in stock: <h:outputText value="#{pr.stock}"/><br/>                            
        price: <h:outputText value="#{pr.price}"/> SEK<br/><br/>                            
        <h:outputText value="#{pr.description}"/><br/></p>                            
        <h:commandButton value="add to cart" action="#{productController.doAddCart(pr.product_name)}"/>                          
    </h:column>                    
</h:dataTable>                
</h:form>

Solution

  • Your managed bean should be SessionScope to live during the session.

    In your case you always creating new ProductController bean for each request and because of that you always inject different CartSessionBean (there is no way how could container know that it should inject the same SessionBean into your RequestScope Managed Bean).