2

I'm trying to pass a parameter to an other page. I have a category of images and that list is in a session scope, a link is created for each of this category in my header template.

I would like to redirect to a new page, and pass the selected category.

<h:dataTable id="categoryMenu" value="#{menuBean.listCategory}" var="category">
    <h:column>
        <h:link value="#{category.name}"
     outcome="/image/imageList.xhtml" >
        </h:link>
    </h:column>
</h:dataTable>

But now I would like that my page imageList.xhtml associated with a ImageListBean.java get the selected category.

I tried a lot of things by requesting GET parameters with f:param, but because my ImageListBean.java must be as view scoped it does not work.

markand
  • 495
  • 1
  • 4
  • 16

2 Answers2

8

Your ViewScoped bean shouldn't be problem. With this code:

<h:link value="#{category.name}" outcome="/image/imageList.xhtml">
  <f:param name="categoryName" value="#{category.name}"/>
</h:link>

you should be able to access this parameter in your backing bean with this:

FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("categoryName")
NawaMan
  • 25,129
  • 10
  • 51
  • 77
partlov
  • 13,789
  • 6
  • 63
  • 82
2

Have you tried viewParam on imageList.xhtml?

<f:metadata>
    <f:viewParam name="id" value="#{bean.id}" />
</f:metadata>

It does basically the following:

  • Get the request parameter value by name id.
  • Convert and validate it if necessary (you can use required, validator and converter attributes and nest a and in it like as with )
  • If conversion and validation succeeds, then set it as a bean property represented by #{bean.id}

You could pass the category id on outcome link (imageList.xhtml?id=1, for example) and retrieve it on your ImageListBean, once you have the "id" parameter you could get the Category searching on your DB.

Diogo Moreira
  • 1,082
  • 2
  • 9
  • 24