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.

Download Source Code

Download it – JSF-2-Implicit-Navigation-Example.zip (10KB)

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
13 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
13 Comment authors
blablaahmed mohdDakMohamed M MowzoonJSF 2.0 Tutorial Recent comment authors
newest oldest most voted
ahmed mohd
Guest
ahmed mohd

Also, another navigation method that uses get request and causes redirect is:

Dak
Guest
Dak

Simple Thank you!

Mohamed M Mowzoon
Guest
Mohamed M Mowzoon

Thanks for the Tutorial MKYong , It is a good starter :) !

trackback
JSF 2.0 Tutorial

[…] Implicit Navigation in JSF 2.0 Now, JSF 2 come out a new “auto view page resolver” mechanism named “implicit navigation“, where you don’t need to declare the above navigation rule, instead, just put the “view name” in the action attribute and JSF will find the correct “view page” automatically. […]

Christiam Hernandez
Guest
Christiam Hernandez

Hey, really nice tutorials, but i have a question, with implicit navigation it is still necessary to use navigation rules to avoid any person get into a page via URL ??

thx in advance :)

armen
Guest
armen

Hi All, I have one question can you provide full example facebook like button which like current page with possible parameters and post to facebook title and correct description.
in jsf site I could not have this result, I tried a lot . Also my content is escaped because it is added by editor, but show in the page with escape false, but facebook get it and show with html tags.
If you can fully help please answer me, it can help also another developers.
You can publish also your answer in JSF facebook page.
http://www.facebook.com/pages/Java-Server-Faces/153580591355347

krishna moorthy
Guest
krishna moorthy

Hi , I’m new to primefaces and your article is really great for the new entry for the technology. I’ve fewer doubts in navigation. I’ve a xhtml page and made a search and navigated into the resultset and am trying to edit the data and update the record and i need to move back to the search screen after update. I’ve designed the screen for search, and to display the resultset and then I’ve edited and updated the data but am unable to get back to the previous screen. when I try to return the search.xhtml , it thows the… Read more »

meer
Guest
meer

Thank you very much,

But, none of these methods worked for me, I don’t know what the problem is. I don’t get errors, just by clicking on the button nothing happens

blabla
Guest
blabla

you needs to declare h:head tags on second page

Usha
Guest
Usha

How to handle page wise and component wise authorization in JSF? Authorization(Right) details come from DB.

ibliss
Guest
ibliss

Is it possible to run a method before navicatlon and does jsf support from action tag

?

Chandan Sharma
Guest
Chandan Sharma

Thanks for this post.. Really useful for me as I am a beginner in JSF 2 :)

Rodrigo
Guest
Rodrigo

Hi,

Great post. I have a doubt about JSF 2 and FaceMessage. I try to show a message before made a redirect but doesn’t work. When I click on Save Button, all works fine and redirect occurs to the specific page without show the success message.

Best Regards,
Rodrigo

Fernando Franzini
Guest
Fernando Franzini

JSF XHTML pages does not invoke “FORWARD” because o facelets life cicle…..you can test build a servlet filter with dispacher FORWARD!!!!