JSF 2带有非常灵活的条件导航规则,可以解决复杂的页面导航流程,请参见以下条件导航规则示例:
1. JSF页面
一个简单的JSF页面,带有一个按钮,可以从该页面移至付款页面。
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>
2.托管豆
提供示例数据以在导航规则中执行条件检查的托管Bean。
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
@ManagedBean
@SessionScoped
public class PaymentController implements Serializable {
private static final long serialVersionUID = 1L;
public boolean registerCompleted = true;
public int orderQty = 99;
//getter and setter methods
}
3.条件导航规则
通常,您在“ faces-config.xml”中声明了简单的导航规则,如下所示:
<navigation-rule>
<from-view-id>start.xhtml</from-view-id>
<navigation-case>
<from-outcome>payment</from-outcome>
<to-view-id>payment.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
使用JSF 2,您可以在移至付款页面之前添加一些条件检查,请参阅以下内容:
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-outcome>payment</from-outcome>
<if>#{paymentController.orderQty < 100}</if>
<to-view-id>ordermore.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>payment</from-outcome>
<if>#{paymentController.registerCompleted}</if>
<to-view-id>payment.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>payment</from-outcome>
<to-view-id>register.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
这等于以下Java代码:
if (from-view-id == "start.xhtml"){
if(from-outcome == "payment"){
if(paymentController.orderQty < 100){
return "ordermore";
}else if(paymentController.registerCompleted){
return "payment";
}else{
return "register";
}
}
}
该代码应具有足够的自我解释性。
注意
在条件导航规则中,导航规则的顺序确实会影响导航流程,始终将最高检查优先级放在顶部。
4.测试
用于测试的不同数据集:
例子1
public class PaymentController implements Serializable {
public boolean registerCompleted = true;
public int orderQty = 99;
...
单击该按钮时,它将达到“ paymentController.orderQty <100 ”标准,并移至“ ordermore.xhtml”页面。
例子2
public class PaymentController implements Serializable {
public boolean registerCompleted = true;
public int orderQty = 200;
...
单击该按钮时,它将达到“ paymentController.registerCompleted ”条件,并移至“ payment.xhtml”页面。
例子3
public class PaymentController implements Serializable {
public boolean registerCompleted = false;
public int orderQty = 200;
...
单击该按钮时,它不符合所有检查条件,并移至“ register.xhtml”页面。
建议
在JSF 2.0中,条件导航规则中没有“ else ”标签,希望JSF团队可以在将来的版本中包括“ else”标签。 例如,
<navigation-rule>
<from-view-id>start.xhtml</from-view-id>
<navigation-case>
<from-outcome>payment</from-outcome>
<if>#{paymentController.orderQty < 100}</if>
<to-view-id>ordermore.xhtml</to-view-id>
<else if>#{paymentController.registerCompleted}</else if>
<to-view-id>payment.xhtml</to-view-id>
<else>
<to-view-id>register.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
而且,它也应该包括多重条件检查,像这样
<navigation-rule>
<from-view-id>start.xhtml</from-view-id>
<navigation-case>
<from-outcome>payment</from-outcome>
<if>#{paymentController.orderQty < 100} && #{paymentController.xxx}</if>
<to-view-id>ordermore.xhtml</to-view-id>
<else if>#{paymentController.registerCompleted}</else if>
<to-view-id>payment.xhtml</to-view-id>
<else>
<to-view-id>register.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
思想…
JSF 2条件导航规则,……与Spring Web Flow非常相似,对吧? 🙂
下载源代码
下载它– JSF-2-Conditional-Navigation-Example.zip (11KB)
翻译自: https://mkyong.com/jsf2/conditional-navigation-rule-in-jsf-2-0/