JSF Tutorial - JSF Form-Action-Navigation Example








JSF provides navigation resolution option even if managed bean different methods returns same view name.

We can define the view page name in faces-config.xml file.

For example the following two methods from UserBean managed bean return the same result.

@ManagedBean
@SessionScoped
public class UserBean implements Serializable {
 
  private static final long serialVersionUID = 1L;
 
  public String processPage1(){
    return "success";
  }
  
  public String processPage2(){
    return "success";
  }

To resolve views, define following navigation rule in faces-config.xml

<navigation-rule>
   <from-view-id>home.xhtml</from-view-id>
   <navigation-case>
      <from-action>#{userBean.processPage1}</from-action>
      <from-outcome>success</from-outcome>
      <to-view-id>page1.jsf</to-view-id>
   </navigation-case>
   <navigation-case>
      <from-action>#{userBean.processPage2}</from-action>
      <from-outcome>success</from-outcome>
      <to-view-id>page2.jsf</to-view-id>
   </navigation-case>
</navigation-rule>

The following JSF page calls the user bean methods from the command button.

<h:form>
  <h:commandButton action="#{userBean.processPage1}" value="Page1" />
  <h:commandButton action="#{userBean.processPage2}" value="Page2" />
</h:form>

Here when Page1 button is clicked.

userBean.processPage1() is called which will return view as success.

JSF will resolve the view name to the corresponding view file page1.xhtml in the current directory.





Example

The following code is from page1.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">
 
    <h:body>
      <h2>This is page1.xhtml</h2>
    </h:body>
</html>

The following code is from start.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">
 
    <h:body>
      <h2>This is start.xhtml</h2>
 
    <h:form>
        <h:commandButton action="#{userBean.processPage1}" value="Page1" />
        <h:commandButton action="#{userBean.processPage2}" value="Page2" />
    </h:form>
 
    </h:body>
</html>

The following code is from UserBean.java.

package com.java2s.common;


import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
import java.io.Serializable;
 
@ManagedBean
@SessionScoped
public class UserBean implements Serializable {
 
  private static final long serialVersionUID = 1L;
 
  public String processPage1(){
    return "success";
  }
  
  public String processPage2(){
    return "success";
  }
  
}

The following code is from page2.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">
 
    <h:body>
      <h2>This is page2.xhtml</h2>
    </h:body>
</html>


Download Form-Action-Navigation.zip





To RUN

Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.

After Tomcat finish starting, type the following URL in the browser address bar.

http://localhost:8080/simple-webapp/start.xhtml