JSF Form-Action导航示例

2018-02-22 15:37 更新

JSF教程 - JSF Form-Action导航示例


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

我们可以在faces-config.xml文件中定义视图页面名称。

例如,来自UserBean托管bean的以下两个方法返回相同的结果。

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

要解析视图,请在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>

以下JSF页面从命令按钮调用用户bean方法。

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

这里,当点击Page1按钮。

调用 userBean.processPage1(),将返回视图成功

JSF将视图名称解析为当前目录中相应的视图文件page1.xhtml


例子

以下代码来自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>

以下代码来自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>

下面的代码来自UserBean.java。

package cn.w3cschool.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";
  }
  
}

以下代码来自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>
下载 Form-Action-Navigation.zip

运行

将生成的WAR文件从目标文件夹复制到Tomcat部署文件夹,并运行Tomcat-Install-folder/bin/startup.bat。

Tomcat完成启动后,在浏览器地址栏中键入以下URL。

http://localhost:8080/simple-webapp/start.xhtml
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号