JSF implicit navigation rule

JSF 2 framework provides the facility of implicit navigation for which no need to define navigation rules and next view page will be resolved automatically. When action returns a string, JSF framework embed .xhtml at the last to the returned string and search for the view page in the current directory.

Note: An action can directly return a string or can call a managed bean method which returns a string.

JSF implicit navigation when action directly returns a string.

Example explanation:

When “Say Hello” button is clicked it returns the “welcome” string. JSF framework embed the .xhtml at the end of the return string and display the welcome.xhtml page.

Example:

test.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
	<title>JSF implicit navigation example.</title> 
    </h:head>
    <h:body> 
      <h3>JSF implicit navigation example.</h3>
      <h:form>
         <h:commandButton value="Say Hello" action="welcome"/>
      </h:form>
    </h:body>
 
</html>

faces-config.xml

<?xml version="1.0" encoding="windows-1252"?>
<faces-config version="2.0" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xi="http://www.w3.org/2001/XInclude" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
 
</faces-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    	xmlns="http://java.sun.com/xml/ns/javaee" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 
    <servlet>
        <servlet-name>faces</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>faces</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
 
</web-app>

welcome.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets">
     <h:head>
	<title>JSF implicit navigation example.</title>
    </h:head>
    <h:body> 
      <h3>JSF implicit navigation example.</h3>
      <h3>Hello World.</h3>
    </h:body>
 
</html>

URL:

http://localhost:7001/JSFExample25/faces/test.xhtml

Output:

JSF example25-1
Click on “Say Hello” button.
JSF example25-2
 
Download this example.