The navigation is one of the powerful points that makes the development of the web application more easier. From the day one JSF provides a built-in navigation mechanism that allow moving from one view to another. The navigation handler is responsible for selecting the next JSF view, when the user has posted the form back into server. Since JSF 2.0, new features were evolved for making the navigation handler much smarter. Static Navigation is one of the navigation techniques, it isn’t new for JSF 2.0, it’s already exist at the jsf 1.2 but jsf 1.1 doesn’t support this navigation.
Also Read:
1. Index View
index.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <h:outputScript library="javax.faces" name="jsf.js"/> </h:head> <h:body> <f:view> <h1>JavaBeat JSF 2.2 Examples</h1> <h2>JSF2 Static Navigation Example</h2> <h:form prependId="false"> <h:outputText value="This is An Index View !"/> <br/> <h:commandButton value="Go To Next View Via Static Navigation" action="nextView"/> </h:form> </f:view> </h:body> </html>
2. Next View
nextView.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <h:outputScript library="javax.faces" name="jsf.js"/> </h:head> <h:body> <f:view> <h1>JavaBeat JSF 2.2 Examples</h1> <h2>JSF2 Static Navigation Example</h2> <h:form prependId="false"> <h:outputText value="Next View !"/> </h:form> </f:view> </h:body> </html>
3. JSF Faces Configuration
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" version="2.2"> <application> <resource-bundle> <base-name>net.javabeat.jsf.application</base-name> <var>msg</var> </resource-bundle> </application> </faces-config>
4. The Deployment Descriptor (web.xml)
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5" metadata-complete="true"> <context-param> <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2 </description> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param> <context-param> <param-name>javax.faces.application.CONFIG_FILES</param-name> <param-value>/WEB-INF/faces-config.xml</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <listener> <listener-class>com.sun.faces.config.ConfigureListener</listener-class> </listener> </web-app>
5. JSF2 Static Navigation Demo
The below snapshots shows you the using of Static Navigation for navigating from index.xhtml to nextView.xhtml.
You have to notice the following points, when you come to use the Static Navigation for achieving your navigation:
- The value of the action attribute is called the outcome, the outcome is nextView.
- An outcome can be optionally mapped to a view ID, in the jsf specification, a jsf page is called view.
If you don’t provide such mapping for a particular outcome, the outcome is transformed into a view ID, using the following steps:
- If the outcome doesn’t have a file extension, then append the extension of the current view.
- If the outcome doesn’t start with a /, then prepend the path of the current view ID /nextView.xhtml.
- The mapping from outcomes to view ID is optional since jsf2.0, you had to specify explicit navigation rules for every outcome.
- One issue must be mentioned here, that is regardless the outcome of the action has been provided inline or by a managed bean property the result will be the same.