Navigations can be handled in JSF by writing methods in the managed bean. These methods should be public, take no parameters and should returns an object or a view name. The method is invoked in the action attribute of the JSF page.

Let’s understand this concept more clearly with an example.

Create addmob.xhtml as

addmob.xhtml

Here we are invoking the add method of the mobile managed bean in the action attribute to render the page on click of submit.

Create viewmob.xhtml that is called from the add method of the bean and displayed.

viewmob.xhtml

Create the managed bean Mobile.java as;

Note that we are returning the viewmob page in the add method which displays the details of the mobile entered by the user upon click of submit.

Now run the application and you should see below response pages.

JSF-Action-Method-Navigation-1-450x275

On clicking submit button in above page, you should get below output.

JSF-Action-Method-Navigation-1-450x275

Another way of handling navigation through a method is by specifying a string outcome in the method and map the returned string to a JSF page. This is done by making an entry in faces-config.xml file.

Create addmobstring.xhtml as;

addmobstring.xhtml

Create viewmobstring.xhtml as

viewmobstring.xhtml

Create the managed bean MobileBean.java as;

Here we are returning the string “for” from the add method.

Now lets create the faces-config.xml as;

If we run the application , we get the expected behavior as shown in below images.

JSF-Action-Method-Navigation-3-450x250

Upon clicking submit button, you should see below response page.

JSF-Action-Method-Navigation-4-450x173

Finally, below image shows the project structure in Eclipse.

JSF-Login-Logout-Authentication-Project

Please download the project zip from below link and play around with it to learn more.

By admin

Leave a Reply