Navigation between different pages of a JavaServer Faces application,
such as choosing the next page to be displayed after a button or link
component is clicked, is defined by a set of rules. Navigation rules can
be implicit, or they can be explicitly defined in the application
configuration resource file. For more information on implicit navigation
rules, see Navigation Model.
Each navigation rule specifies how to navigate from one page to another
page or set of pages. The JavaServer Faces implementation chooses the
proper navigation rule according to which page is currently displayed.
After the proper navigation rule is selected, the choice of which page
to access next from the current page depends on two factors:
The outcome can be anything the developer chooses, but Table
16-3 lists some outcomes commonly used in web applications.
Table 16-3 Common Outcome Strings
Outcome |
What It Means |
success
|
Everything worked. Go on to the next page. |
failure
|
Something is wrong. Go on to an error page. |
login
|
The user needs to log in first. Go on to the login page. |
no results
|
The search did not find anything. Go to the search page
again. |
Usually, the action method performs some processing on the form data of
the current page. For example, the method might check whether the user
name and password entered in the form match the user name and password
on file. If they match, the method returns the outcome success.
Otherwise, it returns the outcome failure. As this example
demonstrates, both the method used to process the action and the outcome
returned are necessary to determine the correct page to access.
Here is a navigation rule that could be used with the example just
described:
<navigation-rule>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-action>#{LoginForm.login}</from-action>
<from-outcome>success</from-outcome>
<to-view-id>/storefront.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{LoginForm.logon}</from-action>
<from-outcome>failure</from-outcome>
<to-view-id>/logon.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
This navigation rule defines the possible ways to navigate from
login.xhtml. Each navigation-case element defines one possible
navigation path from login.xhtml. The first navigation-case says
that if LoginForm.login returns an outcome of success, then
storefront.xhtml will be accessed. The second navigation-case says
that login.xhtml will be re-rendered if LoginForm.login returns
failure.
The configuration of an application’s page flow consists of a set of
navigation rules. Each rule is defined by the navigation-rule element
in the faces-config.xml file.
Each navigation-rule element corresponds to one component tree
identifier defined by the optional from-view-id element. This means
that each rule defines all the possible ways to navigate from one
particular page in the application. If there is no from-view-id
element, the navigation rules defined in the navigation-rule element
apply to all the pages in the application. The from-view-id element
also allows wildcard matching patterns. For example, this from-view-id
element says that the navigation rule applies to all the pages in the
books directory:
<from-view-id>/books/*</from-view-id>
A navigation-rule element can contain zero or more navigation-case
elements. The navigation-case element defines a set of matching
criteria. When these criteria are satisfied, the application will
navigate to the page defined by the to-view-id element contained in
the same navigation-case element.
The navigation criteria are defined by optional from-outcome and
from-action elements. The from-outcome element defines a logical
outcome, such as success. The from-action element uses a method
expression to refer to an action method that returns a String, which
is the logical outcome. The method performs some logic to determine the
outcome and returns the outcome.
The navigation-case elements are checked against the outcome and the
method expression in the following order.
-
Cases specifying both a from-outcome value and a from-action
value. Both of these elements can be used if the action method returns
different outcomes depending on the result of the processing it
performs.
-
Cases specifying only a from-outcome value. The from-outcome
element must match either the outcome defined by the action attribute
of the javax.faces.component.UICommand component or the outcome
returned by the method referred to by the UICommand component.
-
Cases specifying only a from-action value. This value must match
the action expression specified by the component tag.
When any of these cases is matched, the component tree defined by the
to-view-id element will be selected for rendering.