JSF Page Navigation - JavaServer Faces (JSF)

What are JSF Navigation rules?

JSF framework facilitates rules describing the type of view to be shown on the click of a button or link. These rules provided by JSF are Navigation rules.

Navigation rules are defined in the JSF configuration file faces-congig.xml. Managed beans can also define Navigation rules.

Based on the conditions of the navigation rules, the resulted view is shown. JSF 2.0 has implicit navigation and defining of the navigation rules is not required.

What is JSF Implicit Navigation?

Implicit navigation is the auto view page resolver mechanism provided by JSF 2.0. Only view name is to be provided in the action attribute and the correct view page is automatically searched by JSF.

JSF Implicit Navigation

How Auto Navigation is done in JSF page?

Set view name in the action attribute of any JSF UI component.

Here, when Page2 button is clicked, JSF will resolve the view name, page2 as page2.xhtml extension, and find the corresponding view file page2.xhtml in the current directory.

JSF Auto Navigation

How Auto Navigation is done in Managed Bean?

A method to return a view name is defined in managed bean.

Using managed bean, get the view name in action attribute of the JSF UI component.

Here, when Page1 button is clicked, JSF will resolve the view name, page1 as page1.xhtml extension, and find the corresponding view file page1.xhtml in the current directory.

Managed Bean Auto Navigation

What is Conditional Navigation?

The navigation can be easily controlled by using managed bean. Look at the following code in managed bean.

Conditional Navigation

PageId is passed as a request parameter in JSF UI component.

On the click of “Page 1” button:

  • A request with parameter pageId=1 is created by JSF.
  • The parameter is passed to the managed property pageId of navigation Controller by JSF.
  • Now navigationController.showPage() is called which will return view as page1 after checking the pageId
  • The view name, page 1 is resolved as page1.xhtml extension by JSF
  • In the current directory, identify the corresponding view file page1.xhtml
Conditional Navigation

How to resolve the JSF Navigation based on from-action?

Navigation resolution option is provided by JSF even if managed bean different methods return the same view page.

From-Action Navigation

Look at the following code in a managed bean.

To resolve views, define the following navigation rules in faces-config.xml

On the click of page 1 button:
  • navigationController.processPage1() is called which will return view as page

  • JSF will resolve the view name, page1 as view name is page and from-action in faces-config is navigationController.processPage1

  • Find the corresponding view file page1.xhtml in the current directory

    From-action Navigation

What is Forward vs Redirect JSF Navigation?

Server page forward is by default performed by JSF while navigating to another page but the URL of the application will not change.

To enable the page redirection, append faces-redirect=true at the end of the view name.

Forward Vs Redirection Navigation

Here, when Page1 button under Forward is clicked, you will get the following result.

Page 1 under Forward Navigation

Here when Page1 button under Redirect is clicked, you will get the following result.

Page 1 under Redirect Navigation


Example Application

A test JSF application is created to test the above navigation examples.

Step
Description
1
Create a project with a namehelloworldunder apackage com.wisdomjobs.testas explained in theJSF - Create Applicationchapter.
2
CreateNavigationController.javaunder a packagecom.wisdomjobs.testas explained below.
3
Createfaces-config.xmlunder aWEB-INFfolder and updated its contents as explained below.
4
Updateweb.xmlunder aWEB-INFfolder as explained below.
5
Createpage1.xhtmlandpage2.xhtmland modifyhome.xhtmlunder awebappfolder as explained below.
6
Compile and run the application to make sure business logic is working as per the requirements.
7
Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver.
8
Launch your web application using appropriate URL as explained below in the last step.

NavigationController.java

faces-config.xml

web.xml

page1.xhtml

page2.xhtml

home.xhtml

Once all the changes are done, the application is compiled and run as in JSF – Create Application chapter. On successful creation of the application, the result appears as:

JSF Navigation

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JavaServer Faces (JSF) Topics