Main Tutorials

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 Author

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

Comments

Subscribe
Notify of
14 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
ahmed mohd
9 years ago

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

Mario Moreno
4 years ago

Thanks for the article!

Dak
9 years ago

Simple Thank you!

Mohamed M Mowzoon
11 years ago

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

Christiam Hernandez
11 years ago

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
11 years ago

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
11 years ago

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 execption that the objects are already in the session.
I tried with history.go(-1) in onclick … Am more intrested to navigate with jsf rather than going with ajax.
Please help me out to get better understanding and navigate to previous screen in the session as per my requirement.

Thanks in advance!!!

meer
11 years ago

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
9 years ago
Reply to  meer

you needs to declare h:head tags on second page

Usha
12 years ago

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

ibliss
12 years ago

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

?

Chandan Sharma
12 years ago

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

Rodrigo
12 years ago

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
12 years ago

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