From an action in my bean, I'm trying to redirect to another page expecting a view parameter. What is the recommended way to do this in JSF2?
E.g., say my source page is: http://localhost/page1.xhtml
it has a commandButton that calls an action:
<h:commandButton value="submit" action="#{myBean.submit}" />
where my bean looks like:
@ManagedBean @RequestScoped public class MyBean { private int id; public String submit() { //Does stuff id = setID(); return "success"; }
And now, I want the 'submit' action's return to navigate to http://localhost/page2.xhtml?id=2
I've tried to do this with a view-param in my navigation case, but with odd results. The faces-config snippet looks like the following:
<navigation-rule> <from-view-id>/page1.xhtml</from-view-id> <navigation-case> <from-outcome>success</from-outcome> <to-view-id>/page2.xhtml</to-view-id> <redirect> <view-param> <name>id</name> <value>#{myBean.id}</value> </view-param> </redirect> </navigation-case> </navigation-rule>
The weird behaviour being, even though myBean is set to request scoped, it only calls myBean.getId()
the first time I load my application, and reuses that same value for all subsequent calls, producing incorrect view parameters for page2.
So I'm looking for either a better way to do this, or a reason/solution for why the view-param is not being requested from my bean each time.
The unintuitive thing about passing parameters in JSF is that you do not decide what to send (in the action), but rather what you wish to receive (in the target page).
When you do an action that ends with a redirect, the target page metadata is loaded and all required parameters are read and appended to the url as params.
Note that this is exactly the same mechanism as with any other JSF binding: you cannot read inputText's value from one place and have it write somewhere else. The value expression defined in viewParam is used both for reading (before the redirect) and for writing (after the redirect).
With your bean you just do:
@ManagedBean @RequestScoped public class MyBean { private int id; public String submit() { //Does stuff id = setID(); return "success?faces-redirect=true&includeViewParams=true"; } // setter and getter for id
If the receiving side has:
<f:metadata> <f:viewParam name="id" value="#{myBean.id}" /> </f:metadata>
It will do exactly what you want.
Without a nicer solution, what I found to work is simply building my query string in the bean return:
public String submit() { // Do something return "/page2.xhtml?faces-redirect=true&id=" + id; }
Not the most flexible of solutions, but seems to work how I want it to.
Also using this approach to clean up the process of building the query string: http://www.warski.org/blog/?p=185
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With