Implicit Navigation in JSF 2.0

In JSF 1.2, all the page navigation are required to declare in the “faces-config.xml” file like this :

...
<navigation-rule>
   <from-view-id>page1.xhtml</from-view-id>
   <navigation-case>
       <from-outcome>page2</from-outcome>
       <to-view-id>/page2.xhtml</to-view-id>
   </navigation-case>
</navigation-rule>
...

In JSF 2, it treats “outcome” as the page name, for example, navigate to “page1.xhtml”, you have to put the “outcome” as “page1″. This mechanism is called “Implicit Navigation“, where you don’t need to declare the tedious navigation rule, instead, just put the “outcome” in the action attribute directly and JSF will find the correct “view id” automatically.

There are two ways to implements the implicit navigation in JSF 2.

1. Outcome in JSF page

You can put the “outcome” directly in the JSF page.

page1.xhtml – A JSF page with a command button to move from current page to “page2.xhtml”.

<h:form>
    <h:commandButton action="page2" value="Move to page2.xhtml" />
</h:form>

Once the button is clicked, JSF will merge the action value or outcome, “page2” with “xhtml” extension, and find the view name “page2.xhtml” in the current “page1.xhtml” directory.

2. Outcome in Managed Bean

Besides, you can also define the “outcome” in a managed bean like this :

PageController.java

@ManagedBean
@SessionScoped
public class PageController implements Serializable {

    public String moveToPage2(){
        return "page2"; //outcome
    }
}

In JSF page, action attribute, just call the method by using “method expression“.

page1.xhtml

<h:form>
    <h:commandButton action="#{pageController.moveToPage2}" 
    value="Move to page2.xhtml by managed bean" />
</h:form>

Redirection

By default, JSF 2 is perform a forward while navigating to another page, it caused the page URL is always one behind :). For example, when you move from “page1.xhtml” to “page2.xhtml”, the browser URL address bar will still showing the same “page1.xhtml” URL.

To avoid this, you can tell JSF to use the redirection by append the “faces-redirect=true” to the end of the “outcome” string.

<h:form>
    <h:commandButton action="page2?faces-redirect=true" value="Move to page2.xhtml" />
</h:form>

Note
For simple page navigation, this new implicit navigation is more then enough; For complex page navigation, you are still allow to declare the page flow (navigation rule) in the faces-config.xml file.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值