7

I have simple jsf page with view params and load method which is processing those params:

<f:metadata>
 <f:viewParam name="param1" value="#{bean.param1}"/>
 <f:viewParam name="param2" value="#{bean.param2}"/>
 <f:viewParam name="param3" value="#{bean.param3}"/>
 <f:event type="preRenderView" listener="#{bean.load()}"/>
</f:metadata>

I also set some initial values in @PostConstruct. How to redirect user to new location that include those parameters (which are not null). For example user enter in browser:

domain.com/page.jsf

and is redirected to:

domain.com/page.jsf?param1=valueA

because param1 was set in @PostConstruct.

Another question - I have links on page referencing same view:

<h:link value="clickme">
 <f:param name="param3" value="otherValue"/>
</h:link>

When user enters page with ?param1=someValue and clicks link, got redirected to ?param3=otherValue but I want to redirect to ?param1=someValue&param3=otherValue. I know I can add more parameters in <h:link> but it's diffucult to add every possible param in every <h:link>

PS. I use BalusC tip from this topic JSF 2 and Post/Redirect/Get?

Community
  • 1
  • 1
karolkpl
  • 2,189
  • 10
  • 39
  • 60

1 Answers1

13

As to the 1st question: you can add includeViewParams=true to the navigation case outcome. But you can never guarantee that you'll sucessfully be redirected while you're doing that inside a preRenderView method. It might be already too late then.

As to the 2nd question: you can set the includeViewParams attribute of <h:link> to true.

<h:link value="clickme" includeViewParams="true">
    <f:param name="param3" value="otherValue"/>
</h:link>

Alternatively, you can also add includeViewParams=true to the outcome.

<h:link value="clickme" outcome="otherPage?includeViewParams=true">
    <f:param name="param3" value="otherValue"/>
</h:link>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for answer, but I use your tip from http://stackoverflow.com/questions/5416070/jsf-2-and-post-redirect-get/5419730#5419730, I also added `outcome += "&includeViewParams=true";` in `handleNavigation()`, but I'll try use `includeViewParams` at `h:link` – karolkpl Sep 01 '11 at 08:30
  • As to 1st question: user enters `page.jsf` explicitly in browser, not using any link, is it possible to redirect in this case? – karolkpl Sep 01 '11 at 08:33
  • OK,`includeViewParams` inside `h:link` helped (wonder why doing this in `handleNavigation()` dosen't work), 1st question is still open – karolkpl Sep 01 '11 at 09:36
  • does the bean need to to use @Inject? – Thufir Apr 12 '12 at 11:36
  • @thufir: no, it serves a completely different purpose. – BalusC Apr 12 '12 at 12:55