JSF“从动作”导航规则示例

在JSF导航规则中,您可能会遇到以下情况:两个单独的操作在单个页面中返回相同的“ 结果 ”。 在这种情况下,您可以使用“ from-action ”元素来区分这两种导航情况。 请参阅以下示例:

1.托管豆

一个托管bean,具有两个返回相同结果的动作–“成功”。

PageController.java

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

2. JSF页面

一个JSF页面,其中2个按钮链接到上述PageController的方法。

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="#{pageController.processPage1}" value="Page1" />
    	<h:commandButton action="#{pageController.processPage2}" value="Page2" />
      </h:form>
 
    </h:body>
</html>

这两个动作将返回相同的“成功”结果,JSF如何确定要去哪里?

3.导航规则

为了解决这个问题,请在“ faces-config.xml ”中定义以下导航规则,并使用“ from-action ”元素来区分相同的“结果”导航案例。

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>start.xhtml</from-view-id>
	<navigation-case>
		<from-action>#{pageController.processPage1}</from-action>
		<from-outcome>success</from-outcome>
		<to-view-id>page1.xhtml</to-view-id>
	</navigation-case>
	<navigation-case>
		<from-action>#{pageController.processPage2}</from-action>
		<from-outcome>success</from-outcome>
		<to-view-id>page2.xhtml</to-view-id>
	</navigation-case>
    </navigation-rule>	
</faces-config>

4.演示

在上述情况下,按钮的工作方式如下:

  1. 单击具有action =“#{pageController.processPage1}”的按钮时,它将返回“成功”结果并移至page1.xhtml
  2. 单击带有action =“#{pageController.processPage2}”的按钮时,它将返回“成功”结果并移至page2.xhtml

下载源代码

下载它– JSF-2-From-Action-Navigation-Example.zip (11KB)

翻译自: https://mkyong.com/jsf2/jsf-form-action-navigation-rule-example/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值