支持bean中不可检索JSF组件值

支持bean中不可检索JSF组件值,jsf,backing-beans,Jsf,Backing Beans,我正在开发一个webshop,在从支持bean中的JSF组件获取输入值时遇到了问题。我有一个datatable,它动态地从数据库表加载记录。我希望用户能够选择他们想要购买的物品数量,然后单击按钮将其添加到购物车中。我使用一个下拉框来允许用户选择一个值,但是无论选择了什么值,我在我的支持bean中得到的值总是1。任何帮助都将不胜感激 下面是jsf页面的代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

我正在开发一个webshop,在从支持bean中的JSF组件获取输入值时遇到了问题。我有一个datatable,它动态地从数据库表加载记录。我希望用户能够选择他们想要购买的物品数量,然后单击按钮将其添加到购物车中。我使用一个下拉框来允许用户选择一个值,但是无论选择了什么值,我在我的支持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>

凯廷根

#{项链.说明}

Prijs:#{项链.价格}欧元

X

下面是支持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;
    }

}
import java.util.List;
导入org.primefaces.component.datatable.datatable;
导入be.petitefolie.site.controller.controllerobjects.Product;
导入be.petitefolie.site.controller.ControllerObject.ProductType;
公共类NecklaceBean扩展了ProductBean{
私人名单项链;
私人整数金额;
私有数据表数据项链;
公共项链豆(){
necklaces=super.listProductsByProductType(ProductType.项链);
}
公共列表getNecklaces(){
归还项链;
}
公共项链(列表项链){
这个。项链=项链;
}
公共整数getAmount(){
退货金额;
}
公共无效设置金额(整数金额){
这个。金额=金额;
}
公共数据表getDataTableNecklaces(){
返回数据表项链;
}
公共无效setDataTableNecklaces(DataTableDataTableNecklaces){
this.dataTableNecklaces=dataTableNecklaces;
}
公共字符串addNecklaceThoppingCart(){
super.addProductToShoppingCart((产品)this.dataTableNecklaces.getRowData(),amount);
返回null;
}
}

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

我相信这是你问题的根源

一种解决方案是创建如下对象:

class ProductPurchase{
    Necklace necklace;
    int amount;
}

列表
更改为
列表
,将下拉列表值绑定为
项链.金额
,并将“添加到购物车”的操作设置为
操作=“#{necklaceBean.addToShoppingCart(项链)”

谢谢你的回答!这种工作方式确实给了我预期的行为。但我还是有点困惑,因为我以前在这个网店的一个版本中实现过这种工作方式(在那里我使用了JSF 1.2)我现在使用的是JSF 2.1 facelets,这个问题似乎现在就出现了。这是否与JSF 2的一种新的工作方式有关?@Wouter我相信在工作版本中一定有一些细微的不同-例如,如果您使用ajax只从一行发送组件,这可能会导致没有代码很难说。