Implicit Navigation in JSF 2.0

来源:互联网 发布:directx12优化 编辑:程序博客网 时间:2024/06/05 01:59

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@SessionScopedpublic 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