JSF conditional navigation rule

Conditional navigation in JSF 2 framework provides the facility to navigate based on some condition. Navigation condition can be easily handled in managed bean action method.

Example explanation:

In this example we have two links Test and Welcome. A parameter “pageId” is defined for the links which is linked with the “pageId” property of the Test backing bean. When any (Test) link is clicked then its parameter value test is set into pageId property of the Test backing bean. When corresponding displayPage() action is called it returns the “test” string. Then JSF frameworks embed the .xhtml at the end of the return string and display the test.xhtml page.

Example:

Test.java

package com.w3schools.business;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
 
/**
 * Managed bean.
 * @author w3schools
 */
@ManagedBean(name="test")
@RequestScoped
public class Test {
	@ManagedProperty(value="#{param.pageId}")
	private String pageId;
 
	public String getPageId() {
		return pageId;
	}
 
	public void setPageId(String pageId) {
		this.pageId = pageId;
	}
 
	public String displayPage() {
		if (pageId == null) {
			return "home";
		}
		if (pageId.equals("test")) {
			return "test";
		} else if (pageId.equals("welcome")) {
			return "welcome";
		} else {
			return "home";
		}
	}	
}

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>

home.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 conditional navigation example.</title> 
	</h:head>
    <h:body> 
      <h3>JSF conditional navigation example.</h3>
      <h3>Home Page.</h3>
	<h:form>
		<h:commandLink action="#{test.displayPage}" value="Test">
			<f:param name="pageId" value="test" />
		</h:commandLink>
		<br/>
		<h:commandLink action="#{test.displayPage}" value="Welcome">
			<f:param name="pageId" value="welcome" />
		</h:commandLink>
	</h:form>
</h:body>
 
</html>

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 conditional navigation example.</title> 
	</h:head>
    <h:body> 
      <h3>Test Page.</h3>
    </h:body>
 
</html>

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 conditional navigation example.</title>
	</h:head>
    <h:body> 
      <h3>Welcome Page.</h3>
    </h:body>
 
</html>

URL:

http://localhost:7001/JSFExample26/faces/home.xhtml

Output:

JSF example26-1
Click on Test link.
JSF example26-2
Click on Welcome link.
JSF example26-3
 
Download this example.