1

My page display list Category Name. i want to when user clicks category name , it will display list product by category name. in this code, i want to passing CateogryId as value of h:inputHidden. it's same as <h:inputText value="#{produtBean.categoryId}"></h:inputText> .

Tkanks you for reading !

Code from 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>

Code from ProductBean

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

1 Answers1

1

The <h:inputHidden> doesn't work that way. The value you attempted to "pass" into it is also kind of weird. It's the same value for every item of the list. You should be using <f:param> instead. You probably also want to pass the #{c.id} or #{c.name} instead.

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

With

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

Alternatively, if you're already on Servlet 3.0 / EL 2.2, then you can just pass it as method argument.

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

with

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

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555