JSF - 页面导航

导航规则是JSF Framework提供的那些规则,用于描述单击按钮或链接时要显示的视图.

可以在JSF配置文件中定义导航规则faces-config.xml中.它们可以在托管bean中定义.

导航规则可以包含可以显示结果视图的条件. JSF 2.0也提供隐式导航,其中不需要定义导航规则.

隐式导航

JSF 2.0提供自动查看页面解析程序名为隐式导航的机制.在这种情况下,您只需要将视图名称放在action属性中,JSF将在已部署的应用程序中自动搜索正确的视图页面.

JSF主页

JSF页面中的自动导航

在任何操作属性中设置视图名称JSF UI组件.

<h:form>
   <h3>Using JSF outcome</h3>
   <h:commandButton action = "page2" value = "Page2" />
</h:form>

在这里,当点击 Page2 按钮时,JSF将解析视图名称 page2 as page2 .xhtml扩展名,并在当前目录中找到相应的视图文件 page2.xhtml .

JSF Page 2

托管Bean中的自动导航

在托管bean中定义一个方法以返回视图名称.

@ManagedBean(name = "navigationController", eager = true)
@RequestScoped

public class NavigationController implements Serializable {
   public String moveToPage1() {
      return "page1";
   }
}

使用托管bean获取任何JSF UI组件的action属性中的视图名称.

<h:form> 
   <h3> Using Managed Bean</h3>  
   <h:commandButton action = "#{navigationController.moveToPage1}" 
   value = "Page1" /glt; 
</h:form>

在这里,当点击 Page1 按钮时,JSF将解析视图名称 page1 as page1 .xhtml扩展名,并在当前目录中找到相应的视图文件 page1.xhtml .

JSF Page 1

条件导航

使用托管bean,我们可以非常轻松地控制导航.查看托管bean中的以下代码.

JSF条件导航


@ManagedBean(name = "navigationController", eager = true)
@RequestScoped

public class NavigationController implements Serializable {
   //this managed property will read value from request parameter pageId
   @ManagedProperty(value = "#{param.pageId}")
   private String pageId;

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

将pageId作为JSF UI组件中的请求参数传递.

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

这里,当点击"Page1"按钮时.

  • JSF将使用参数pageId = 1

  • 创建请求然后JSF将此参数传递给navigationController的托管属性pageId

  • 现在调用navigationController.showPage(),它会在检查pageId后返回view1页面

  • JSF将将视图名称page1解析为page1.xhtml扩展名

  • 在当前目录中查找相应的视图文件page1.xhtml

JSF Page 1

根据以下内容解决导航问题action

JSF提供导航解析选项,即使托管bean不同的方法返回相同的视图名称.

JSF From Action

查看托管bean中的以下代码.

public String processPage1() { 
   return "page"; 
} 
public String processPage2() { 
   return "page"; 
}

要解析视图,请在 faces-config.xml

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

</navigation-rule>

这里,点击Page1按钮 : 

  • 调用

    navigationController.processPage1()将视图返回页面

  • JSF将解析视图名称 page1 视图名称为页面,而 faces-config中的from-action 是navigationController.processPage1

  • 在当前目录中找到相应的视图文件 page1.xhtml

JSF Page 1

转发与重定向

JSF默认执行服务器页面转发导航到另一个页面并且应用程序的URL不会更改.

要启用页面重定向,请在视图末尾附加 faces-redirect = true name.

JSF Forward vs Redirect

<h:form>
   <h3>Forward</h3>
   <h:commandButton action = "page1" value = "Page1" />
   <h3>Redirect</h3>
   <h:commandButton action = "page1?faces-redirect = true" value = "Page1" />
</h:form>

此处,当点击转发下的 Page1 按钮时,您将获得以下结果.

JSF Page 1 Forward

此处 Page1 重定向下的>按钮,您将得到以下结果.

JSF Page 1重定向

示例应用程序

让我们创建一个测试JSF应用程序来测试上述所有导航示例.

Step描述
1包com.it1352.test 下创建一个名为 helloworld 的项目,如 JSF中所述 - 创建应用程序章节.
2在包 com.it1352.test 下创建 NavigationController.java ,如下所述.
3 WEB-INF 文件夹下创建 faces-config.xml 并更新其内容,如下所述.
4更新 web.xml WEB-INF 文件夹下,如下所述.
5创建 page1.xhtml page2.xhtml 并修改 home.xhtml webapp 文件夹下,如下所述.
6编译并运行应用程序以确保业务逻辑按照要求运行.
7最后,以war文件的形式构建应用程序并将其部署在Apache Tomcat Webserver中.
8使用适当的URL启动您的Web应用程序如下面最后一步所述.

NavigationController.java

package com.it1352.test; 
import java.io.Serializable;  

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ManagedProperty; 
import javax.faces.bean.RequestScoped;  

@ManagedBean(name = "navigationController", eager = true) 
@RequestScoped 
public class NavigationController implements Serializable {  
   private static final long serialVersionUID = 1L;  
   @ManagedProperty(value = "#{param.pageId}")    
   private String pageId;  
   
   public String moveToPage1() {      
      return "page1";    
   }  
   
   public String moveToPage2() {       
      return "page2";    
   }  
   
   public String moveToHomePage() {      
      return "home";    
   }  
   
   public String processPage1() {       
      return "page";    
   }  
   
   public String processPage2() {       
      return "page";    
   } 
   
   public String showPage() {       
      if(pageId == null) {          
         return "home";       
      }       
      
      if(pageId.equals("1")) {          
         return "page1";       
      }else if(pageId.equals("2")) {          
         return "page2";       
      }else {          
         return "home";       
      }    
   }  
   
   public String getPageId() {       
      return pageId;    
   }  
   
   public void setPageId(String pageId) {       
      this.pageId = pageId;   
   } 
}

faces-config.xml

<?xml version = "1.0" encoding = "UTF-8"?>

<faces-config
   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-facesconfig_2_0.xsd"
   version = "2.0">
   
   <navigation-rule>
      <from-view-id>home.xhtml</from-view-id>
      <navigation-case>
         <from-action>#{navigationController.processPage1}</from-action>
         <from-outcome>page</from-outcome>
         <to-view-id>page1.jsf</to-view-id>
      </navigation-case>
      <navigation-case>
         <from-action>#{navigationController.processPage2}</from-action>
         <from-outcome>page</from-outcome>
         <to-view-id>page2.jsf</to-view-id>
      </navigation-case>
   </navigation-rule>

</faces-config>

web.xml

<!DOCTYPE web-app PUBLIC
   "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
   "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
   <display-name>Archetype Created Web Application</display-name>

   <context-param>
      <param-name>javax.faces.PROJECT_STAGE</param-name>
      <param-value>Development</param-value>
   </context-param>
   <context-param>
      <param-name>javax.faces.CONFIG_FILES</param-name>
      <param-value>/WEB-INF/faces-config.xml</param-value>
   </context-param>
   
   <servlet>
      <servlet-name>Faces Servlet</servlet-name>
      <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name>Faces Servlet</servlet-name>
      <url-pattern>*.jsf</url-pattern>
   </servlet-mapping>

</web-app>

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</h2>
      <h:form>
         <h:commandButton action = "home?faces-redirect = true"
            value = "Back To Home Page" />
      </h:form>
   </h:body>
</html>

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</h2>
      <h:form>
         <h:commandButton action = "home?faces-redirect = true"
            value = "Back To Home Page" />
      </h:form>
   </h:body>
</html>

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:f = "http://java.sun.com/jsf/core"
   xmlns:h = "http://java.sun.com/jsf/html">

   <h:body>
      <h2>Implicit Navigation</h2>
      <hr />
      
      <h:form>
         <h3>Using Managed Bean</h3>
         <h:commandButton action = "#{navigationController.moveToPage1}"
            value = "Page1" />
         <h3>Using JSF outcome</h3>
         <h:commandButton action = "page2" value = "Page2" />
      </h:form>
      <br/>
      
      <h2>Conditional Navigation</h2>
      <hr />
      <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>
      <br/>
      
      <h2>"From Action" Navigation</h2>
      <hr />
      
      <h:form>
         <h:commandLink action = "#{navigationController.processPage1}"
         value = "Page1" />
              
         <h:commandLink action = "#{navigationController.processPage2}"
         value = "Page2" />
              
      </h:form>
      <br/>
      
      <h2>Forward vs Redirection Navigation</h2>
      <hr />
      <h:form>
         <h3>Forward</h3>
         <h:commandButton action = "page1" value = "Page1" />
         <h3>Redirect</h3>
         <h:commandButton action = "page1?faces-redirect = true"
         value = "Page1" />
      </h:form>
   </h:body>
</html>

完成所有更改之后,让我们像在JSF  -  Create Application章节中那样编译并运行应用程序.如果您的应用程序一切正常,这将产生以下结果.

JSF Navigation