如何将hiddenFiled的值作为actionbean的参数传递?

时间:2012-09-15 09:45:28

标签: jsf

我的页面显示列表类别名称。我想当用户点击类别名称时,它会 按类别名称显示列表产品。在这段代码中,我想传递CateogryId作为h:inputHidden的值。它与<h:inputText value="#{produtBean.categoryId}"></h:inputText>相同。

Tkanks you for reading!

来自xhtml的代码

<ui:repeat value="#{productBean.listCategory}" var="c">
   <h:form>
        <h:inputHidden value="#{productBean.categoryId}" ></h:inputHidden>
        <h:commandLink value="#{c.name}" action="#{productBean.listProductByCt}" ></h:commandLink>
   </h:form>
</ui:repeat>

来自ProductBean的代码

public String listProductByCt()
    {
        if(categoryId==0)
        {
            return "index";
        }
        listProduct = new ProductsDB().listProducts(categoryId);
        return "product";
    }  

1 个答案:

答案 0 :(得分:1)

<h:inputHidden>不起作用。你试图“传递”到它的价值也有点奇怪。它与列表中的每个项目的值相同。您应该使用<f:param>代替。您可能还想要传递#{c.id}#{c.name}

<h:commandLink value="#{c.name}" action="#{productBean.listProductByCt}">
    <f:param name="categoryId" value="#{c.id}" />
</h:commandLink>

使用

@ManagedProperty("#{param.categoryId}")
private Integer categoryId; // +setter

或者,如果您已经在Servlet 3.0 / EL 2.2上,那么您可以将其作为方法参数传递。

<h:commandLink value="#{c.name}" action="#{productBean.listProductByCt(c.id)}">

public String listProductByCt(Integer categoryId) {
    // ...
}

另见: