Navigate to other view passing a parameter

后端 未结 1 2026
无人及你
无人及你 2020-12-18 11:35

My current environment is JRE 1.7, JSF 2.2, Eclipse Luna. In a certain page (entity_index.xhtml) of my application I have the following (PrimeFaces) button:

相关标签:
1条回答
  • 2020-12-18 12:15

    p:commandButton performs a POST request. You want simply to GET a view with your entity detail, not to POST the server, so you need a h:link:

    <h:link value="Details" outcome="entity_details" target="_blank">
        <f:param name="id" value="#{entity.id}" />
    </h:link>
    

    Then, the f:viewParam in the destination page will be able to process the url parameter:

    <f:metadata>
        <f:viewParam name="id" value="#{entityDetailsMB.id}"/>
        <f:viewAction action="#{entityDetailsMB.init}" />
    </f:metadata>
    

    Use a f:viewAction to initialize your entity instead of doing it in a getter, which is discouraged:

    public void init(){
        entity = entityById(id);
    }
    

    See also:

    • Choosing how to pass parameters to a target bean/page using JSF
    • When to use f:viewAction / preRenderView versus PostConstruct?
    0 讨论(0)
提交回复
热议问题