Question

I'm developing a webshop and I'm facing problems with getting input values from JSF components in the backing bean. I have a datatable, which dynamically loads records from a database table. I want the user to be able to select the amount of items they want to buy and click a button to add them to their shoppingcart. I'm using a dropdownbox to allow the user to select a value, but no matter what value is selected, the value I get in my backing bean is always 1. Any help would be greatly appreciated.

Here's the code for the jsf page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<ui:composition template="/WEB-INF/templates/basictemplate.xhtml">
    <ui:define name="content">
        <h:form id="form">
            <p:dataTable var="necklace" value="#{necklaceBean.necklaces}" paginator="true" rows="10" id="necklaceTable" binding="#{necklaceBean.dataTableNecklaces }"
                paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}">

                <f:facet name="header">  
                    Kettingen  
                </f:facet>  

                <p:column headerText="Foto" id="picture">
                    <p:lightBox styleClass="imagebox">
                        <h:outputLink value="#{necklace.picture }" title="#{necklace.productId }">
                            <h:graphicImage url="#{necklace.picture }" width="175" height="116"/>
                        </h:outputLink>
                    </p:lightBox>
                </p:column>

                <p:column headerText="Omschrijving" id="description">  
                    <p>
                        #{necklace.description}
                    </p>
                    <p>
                        Prijs: #{necklace.price} Euro
                    </p>
                    <p>
                        <p:selectOneMenu value="#{necklaceBean.amount}">    
                            <f:selectItem itemLabel="1" itemValue="1" />  
                            <f:selectItem itemLabel="2" itemValue="2" />  
                            <f:selectItem itemLabel="3" itemValue="3" />  
                            <f:selectItem itemLabel="4" itemValue="4" />  
                            <f:selectItem itemLabel="5" itemValue="5" />  
                            <f:selectItem itemLabel="6" itemValue="6" />  
                            <f:selectItem itemLabel="7" itemValue="7" />  
                            <f:selectItem itemLabel="8" itemValue="8" />  
                            <f:selectItem itemLabel="9" itemValue="9" />  
                            <f:selectItem itemLabel="10" itemValue="10" />  
                        </p:selectOneMenu>
                        X
                        <p:commandButton value="Toevoegen aan winkelwagentje" action="#{necklaceBean.addNecklaceToShoppingCart}"
                            disabled="#{necklace.soldOut}" ajax="false" />
                    </p>
                </p:column>
            </p:dataTable>
        </h:form>
    </ui:define>
</ui:composition>
</html>

And here's the backing bean:

import java.util.List;

import org.primefaces.component.datatable.DataTable;

import be.petitefolie.site.controller.controllerobjects.Product;
import be.petitefolie.site.controller.controllerobjects.ProductType;

public class NecklaceBean extends ProductBean {

    private List<Product> necklaces;
    private int amount;
    private DataTable dataTableNecklaces;

    public NecklaceBean(){

        necklaces = super.listProductsByPoductType(ProductType.NECKLACE);

    }

    public List<Product> getNecklaces() {
        return necklaces;
    }

    public void setNecklaces(List<Product> necklaces) {
        this.necklaces = necklaces;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public DataTable getDataTableNecklaces() {
        return dataTableNecklaces;
    }

    public void setDataTableNecklaces(DataTable dataTableNecklaces) {
        this.dataTableNecklaces = dataTableNecklaces;
    }

    public String addNecklaceToShoppingCart(){

        super.addProductToShoppingCart((Product)this.dataTableNecklaces.getRowData(), amount);

        return null;
    }

}
Was it helpful?

Solution

The problem is that you are binding a single bean to the components of each row in your table. If you have multiple rows and in the first one you select 3 in the dropdown box and press "add to shopping cart", then if there is another row with 1 selected in the dropdown box, the bean will get updated to 1 (prcisely: firstly it will get updated according to the first row to 3, then according to the second row to 1, and so on).

I believe that this is the source of your problems.

One solution would be to create an object like:

class ProductPurchase{
    Necklace necklace;
    int amount;
}

change the List<Product> to List<ProductPurchase>, bind the drop down list value to necklace.amount and set the action of "add to cart" to action="#{necklaceBean.addToShoppingCart(necklace)".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top