This is a second post in Learning JSF 2 series. The first one on Managed Beans can be found here.
Before I start, thanks to Nick Belaevski (RichFaces Team Lead – Exadel) for reviewing this posting.
In JSF 1.2 all navigation rules are placed in JSF configuration file. Although you can still places navigation rules inside JSF configuration file, JSF 2 upgrades navigation by introducing implicit navigation and conditional navigation.
Implicit navigation
In JSF 1.2, navigating from one page to another required something like this:
page1.xhtml next /page2.xhtml
JSF 2 now supports implicit navigation where you don’t need to define a navigation rules inside JSF configuration file. You can do this:
JSF will try to find a view named page2.xhtml in the current directory.
The following will also work:
or
Note: this is assuming that JSF servlet is mapped to .jsf.
or using the new h:link (or h:button) tags in JSF 2 (I’ll cover these tags in a separate posting):
Implicit navigation can also be used from within an action method:
public String next () { return "page2"; }
The following will also work:
public String next () { return "page2.jsf"; }
public String next () { return "page2.xhtml"; }
All examples above implied that both pages (page1.xhtml and page2.xhtml) are in the same directory (notice there is no / before page name). If pages are in different directories, then full path has to be used:
or
public String next () { return "/shopping/page2" ; }
Conditional navigation
In JSF 1.2, methods in managed beans would return arbitrary string values which are passed into the navigation. Navigation couldn’t use application state to help determine what page to select. The most you could is something like this, you could check what button/link was clicked in addition to using the outcome:
/pages/course.xhtml #{bean.register} success /pages/registered.xhtml
In JSF 2, you can now do this:
/pages/course.xhtml #{bean.register} #{bean.prerequisiteCompleted} /pages/registered.xhtml #{bean.register} #{bean.advisingHold} /pages/scheduleAdvisingSession.xhtml #{bean.register} #{not bean.payment} /pages/payForCourse.xhtml
When #{bean.register} action is invoked, based on .. condition (evaluates to true/false) in each case, it’s possible to navigate to three different pages. This allows to use application state to determine where to navigate. It’s not longer necessary to have model objects return arbitrary strings and thus could eliminate having your back-end know anything about navigation.
Forward/Redirect
When navigating to another page, both JSF 1.2 and JSF 2 perform a forward (default behavior) to the new page (Seam, for example does a redirect by default). Because it’s a server forward (the browser is not aware that we are displaying a different page), you might have noticed that the page address in the URL is always one behind.
If defining navigation rules in JSF configuration file, then the same tag is used in JSF 1.2 and JSF 2:
/bar/enter.xhtml enterBar /bar/welcome.xhtml
When using implicit navigation in JSF2, redirect is setup using faces-redirect=true request parameter:
If returning an outcome from action method:
public String enter () { return "/bar/welcome?faces-redirect=true"; }
Using EL in to-view-id
You can also use EL in :
/el/page1.xhtml #{bean.navigate} success #{bean.page}
In the managed bean:
Γ’β¬Β¦ private String page; public String getPage () { return this.page; } public String navigate () { this.page = "/el/page3"; return "success"; } Γ’β¬Β¦
A topic closely related to navigation is page parameters and how they are propagated, I will cover that in another posting.
Finally, one thing to be aware of. Suppose you have the following rule:
/purchase.xhtml #{bean.purchase} #{not bean.creditCardExpired} /confirmation.xhtml
In managed bean:
public String purchase () { ... return "confirmation"; }
Suppose creditCardExpired evaluates to true and thus making condition false. In such case you would think that navigation shouldn’t take place. However, you still navigate to confirmation.xhtml because implicit navigation is used. First, the above case is matched but not selected as .. evaluates to false. Navigation continues to look for a match and because purchase() method returned a string value of “confirmation” is used by implicit navigation which matches a page with such name. Implicit navigation is used last.
Leave a Reply to sam Cancel reply