JSF Tutorial - JSF Conditional Navigation Example








With managed bean we can control the navigation based on a condition.

For example, the following code shows how to return different page name for different pageId value.

@ManagedBean(name = "navigationController", eager = true)
@RequestScoped
public class NavigationController implements Serializable {

   //read value from request parameter pageId
   @ManagedProperty(value="#{param.pageId}")
   private String pageId;

   //if pageId is 1 show page1.xhtml,
   //if pageId is 2 show page2.xhtml
   //else show default.xhtml
   public String showPage(){
      if(pageId == null){
         return "default";
      }
      if(pageId.equals("1")){
         return "page1";
      }else if(pageId.equals("2")){
         return "page2";
      }else{
         return "default";
      }
   }
}

Pass pageId as a request parameter in JSF UI Component.

<h:form>
   <h:commandLink action="#{navigationController.showPage}" value="Page1">
      <f:param name="pageId" value="1" />
   </h:commandLink>
   <h:commandLink action="#{navigationController.showPage}" value="Page2">
      <f:param name="pageId" value="2" />
   </h:commandLink>
   <h:commandLink action="#{navigationController.showPage}" value="Home">
      <f:param name="pageId" value="3" />
   </h:commandLink>
</h:form>

Here when "Page1" button is clicked

JSF will create a request with parameter pageId=1. Then JSF will pass this parameter to managed property pageId of navigationController.

After that navigationController.showPage() is called which will return view as page1 after checking the pageId.

JSF then resolve the view name, page1 as page1.xhtml extension and find the corresponding view file page1.xhtml in the current directory.





Example

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

The following code is from ordermore.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 ordermore.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="payment" value="Payment" />
    </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 boolean registerCompleted = true;
  public int orderQty = 99;
  
  public int getOrderQty() {
    return orderQty;
  }

  public void setOrderQty(int orderQty) {
    this.orderQty = orderQty;
  }

  public boolean isRegisterCompleted() {
    return registerCompleted;
  }

  public void setRegisterCompleted(boolean registerCompleted) {
    this.registerCompleted = registerCompleted;
  }
  
}

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


Download Conditional-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