在后台bean中无法检索JSF组件值



我正在开发一个web商店,并且我正面临从支持bean中的JSF组件获取输入值的问题。我有一个数据表,它从数据库表动态加载记录。我希望用户能够选择他们想要购买的物品数量,并单击一个按钮将它们添加到购物车中。我使用下拉框来允许用户选择一个值,但是无论选择什么值,我在后台bean中得到的值总是1。如有任何帮助,我将不胜感激。

下面是jsf页面的代码
<!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>

这是后台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;
    }
}

问题是您将单个bean绑定到表中每行的组件。如果您有多行,并且在第一行中您在下拉框中选择3并按"添加到购物车",那么如果在下拉框中有另一行选择1,则bean将更新为1(准确地说:首先它将根据第一行更新为3,然后根据第二行更新为1,依此类推)。

我相信这就是你问题的根源。一种解决方案是创建如下对象:
class ProductPurchase{
    Necklace necklace;
    int amount;
}

List<Product>更改为List<ProductPurchase>,将下拉列表值绑定为necklace.amount,将"加入购物车"动作设置为action="#{necklaceBean.addToShoppingCart(necklace)"