JSF navigation determines the web application flow from one JSF
page to another.
Navigation takes place when the user clicks a
h:commandButton
or other similar component.
In a JSF page these components have the following form:
<h:commandButton value="label-expression" action="outcome-expression"/>
The
outcome-expression is a string-valued
expression:
- Its value is called an outcome, which can be either static or
dynamic.
- An outcome is eventually translated into a view id, which
identifies the JSF page that is displayed after the button is clicked.
Outcome strings are eventually translated into view ids that identify
the target JSF page.
Part of the translation involves expanding the outcome string into a
path.
-
If the outcome string does not begin with a forward slash ("/") then
the path for the folder containing the current JSF page is added at
the beginning of the outcome string.
-
If the outcome string does not have a filename suffix then the suffix
for the current JSF page is added at the end of the outcome string.
For example, in the
login application's
index.xhtml JSF page:
<h:commandButton value="Login" action="welcome"/>
Since the outcome string "welcome" does not have a suffix, the destination page
welcome.xhtml is assumed.
Since "welcome" does not begin with "/",
welcome.xhtml is assumed to
reside where
index.xhtml resides.
A
logical outcome is not directly mapped to a JSF page:
- Logical outcomes are specified in navigation rules
- Navigation rules are placed in the faces-config.xml file
- Navigation rules specify how logical outcomes are translated to view ids
- The javaquiz application is an example
An outcome expression can invoke static or dynamic navigation.
In static navigation,
- Determination of the next page does not depend on the state of
the application's beans
- The outcome expression is just a string literal
Example from before:
<h:commandButton value="Login" action="welcome"/>
In dynamic navigation, the outcome expression invokes an
action method that, among other uses, allows navigation to depend on
bean state.
An
action method is a bean method that is usually invoked from a
command button in a JSF page.
It returns an outcome string to direct the navigation to the next page.
An action method is needed when either or both of the following are
true.
A JSF page that uses action methods typically contains a tag like the
following.
<h:commandButton value="button-label" action="#{bean-name.method-name(argument-list)}"/>
If literal string arguments are used they must be enclosed in single
quotes.
If the method has no arguments the parentheses can be omitted.
For example, suppose that the
login application has
a
loginController bean with a
method called
verifyUser that needs to be run when the user attempts to
login:
<h:commandButton label="Login" action="#{loginController.verifyUser}"/>
The appropriate bean class for bean-name should contain the
following declaration.
public String method-name(parameter-declarations) {
implementation-code
return "outcome";
}
The
method-name in this declaration should be identical to
the
method-name in the JSF page.
The
outcome should be the name of the next JSF page or a
logical outcome.
For example, here is how the
loginController bean's
verifyUser method might look:
String verifyUser() {
if (...)
return "success";
else
return "failure";
}