JSF from-action navigation rule

As we discussed in previous example JSF can resolve view based on what managed bean method returns but what if two or more managed bean methods returns the same view. To resolve such view requests JSF provides the from action navigation facility. We have to put the entries of view pages into faces-config.xml file with from action attributes.

Syntax:

<navigation-rule>
   <from-view-id>fromViewPage</from-view-id>
   <navigation-case>
      <from-action>#{beanName.method1}</from-action>
      <from-outcome>returnValue</from-outcome>
      <to-view-id>viewPageName1</to-view-id>
   </navigation-case>
   <navigation-case>
      <from-action>#{beanName.method2}</from-action>
      <from-outcome>returnValue</from-outcome>
      <to-view-id>viewPageName2</to-view-id>
   </navigation-case>
</navigation-rule>

Example explanation:

In this example we have two links Test and Welcome. When clicked both links returns the same string “success”. JSF framework resolve this situation with the help of the from-action rule. The from-action matches the bean and method first and then look at the from-outcome. The to-view-id from matching case is the return navigation string. Let we click the Test link. The displayTestPage() action returns the “success” string. JSF framework looks at the navigation rules and return test.xhtml value.

Example:

Test.java

package com.w3spoint.business;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
 
/**
 * Managed bean.
 * @author w3spoint
 */
@ManagedBean(name="test")
@RequestScoped
public class Test {
 
	public String displayTestPage() {
		return "success";
	}
 
	public String displayWelcomePage() {
		return "success";
	}
}

faces-config.xml

<?xml version="1.0" encoding="windows-1252"?>
<?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">
 
<navigation-rule>
   <from-view-id>home.xhtml</from-view-id>
   <navigation-case>
      <from-action>#{test.displayTestPage}</from-action>
      <from-outcome>success</from-outcome>
      <to-view-id>test.xhtml</to-view-id>
   </navigation-case>
   <navigation-case>
      <from-action>#{test.displayWelcomePage}</from-action>
      <from-outcome>success</from-outcome>
      <to-view-id>welcome.xhtml</to-view-id>
   </navigation-case>
</navigation-rule>
 
</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 from action navigation example.</title> 
	</h:head>
    <h:body> 
      <h3>JSF from action navigation example.</h3>
      <h3>Home Page.</h3>
	<h:form>
		<h:commandLink action="#{test.displayTestPage}" value="Test"/>
		<br/>
		<h:commandLink action="#{test.displayWelcomePage}" value="Welcome"/>
	</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 from action 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 from action navigation example.</title>
	</h:head>
    <h:body> 
      <h3>Welcome Page.</h3>
    </h:body>
 
</html>

URL:

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

Output:

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

Please follow and like us:
Content Protection by DMCA.com