首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在支持bean中无法检索JSF组件值

在支持bean中无法检索JSF组件值
EN

Stack Overflow用户
提问于 2013-05-21 00:58:08
回答 1查看 1.1K关注 0票数 0

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

下面是jsf页面的代码

代码语言:javascript
运行
复制
<!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:

代码语言:javascript
运行
复制
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;
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-21 03:40:02

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

我相信这就是你问题的根源。

一个解决方案是创建一个对象,如下所示:

代码语言:javascript
运行
复制
class ProductPurchase{
    Necklace necklace;
    int amount;
}

List<Product>更改为List<ProductPurchase>,将下拉列表值绑定为necklace.amount,并将"add to cart“的操作设置为action="#{necklaceBean.addToShoppingCart(necklace)"

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16654470

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档