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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…