Question

I want to navigate from one page to another page, say from page1 with bean1 to page2 with bean2. I need to pass some parameters from bean1 to bean2.

I would like to understand how to use @ManagedProperty for parameters and <f:viewParam> in <f:metadata> portion of page2. Say, I have field1, field2 and field3 available in bean1 and bean2 with getters and setters. My understanding is that I will have to define view params in Metadata of page2:

Like

<f:metadata>
    <f:viewParam name="field1" value="#{bean2.field1}"/>
    <f:viewParam name="field2" value="#{bean2.field2}"/>
    <f:viewParam name="field3" value="#{bean2.field3}"/>
</f:metadata> 

I am not sure where I use annotations for @ManagedProperty to define the parameters field1, field2 and field3, in bean1 or bean2.

On page1 I can use "page2?faces-redirect=true&amp;includeViewParams=true"

Can I use the same in one of my methods instead in page1, say on response to a submit of commandlink?

If I need to have those three fields in both page1 and page2, can I define those hidden fields?

Was it helpful?

Solution

You need to specify them as <f:param> in the <h:link> of page1.xhtml.

<h:link value="Go to page2" outcome="page2">
    <f:param name="field1" value="#{bean1.field1}" />
    <f:param name="field2" value="#{bean1.field2}" />
    <f:param name="field3" value="#{bean1.field3}" />
</h:link>

You can then use <f:viewParam> (or @ManagedProperty, but this allows less fine grained validation) to set them in bean of page2.xhtml.

<f:metadata>
    <f:viewParam name="field1" value="#{bean2.field1}" />
    <f:viewParam name="field2" value="#{bean2.field2}" />
    <f:viewParam name="field3" value="#{bean2.field3}" />
</f:metadata>

You do not need to send a POST request by <h:commandLink> with faces-redirect and includeViewParam here. Just a simple GET request by <h:link> is much simpler and SEO friendlier.

See also:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top