JSF Passing parameter and redirecting to view scope

丶灬走出姿态 提交于 2019-12-10 10:52:05

问题


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.


回答1:


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")



回答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.



来源:https://stackoverflow.com/questions/14487924/jsf-passing-parameter-and-redirecting-to-view-scope

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!