JSF “from-action” navigation rule example

In JSF navigation rule, you may encounter a situation where two separate actions return a same “outcome” in a single page. In this case, you can use “from-action” element to differentiate the two navigation cases. See following example :

1. Managed Bean

A managed bean, with two actions which return a same outcome – “success”.

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 Page

A JSF page, with 2 buttons linked to the above PageController‘s methods.

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>
    <div><div class="ads-in-post hide_if_width_less_800">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 728x90 - After2ndH4 -->
<ins class="adsbygoogle hide_if_width_less_800" 
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-2836379775501347"
     data-ad-slot="3642936086"
     data-ad-region="mkyongregion"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div></div><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>

Both actions will return the same “success” outcome, how JSF determine where to go?

3. Navigation Rule

To solve it, defines following navigation rules in the “faces-config.xml“, and use the “from-action” element to differentiate the same “outcome” navigation cases.

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

In above case, the button works like this :

  • When button with action=”#{pageController.processPage1}” is clicked, it will return the “success” outcome and move to page1.xhtml
  • When button with action=”#{pageController.processPage2}” is clicked, it will return the “success” outcome and move to page2.xhtml
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值