Java Server Faces: Page Navigation

A web site typically contains a number of pages. A page can navigate another page using

  • Implicit navigation.
  • Navigation rules (including conditional rules) defined in managed beans.
  • Navigation rules defined in JSF configuration file named faces-config.xml.

1. Auto Navigation

The simplest way to refer to a page from another is to use action attribute of <h:commandButton> or <h:commandLink> tag.


<h:commandButton action=”check” value=”Login” />


When this button captioned “Login” is clicked, the request is forwarded to a page check.xhtml which is expected to exist in the same directory of the current page. The <h:commandLink> tag may be used in the same way as follows:


<h:commandLink action=”help” value=”Help” />


Here, when the link captioned “Help” is clicked, the request is forwarded to a page heip.xhtmi.

JSF by default forwards requests to navigated page and the URL of the application does not change. To redirect the request to the navigated page, we can pass the value of the faces-redirect parameter as true as follows:

<h:commandButton action=”check?faces-redirect=true” value=”Login” />

2. Using Managed bean

The value of action attribute may be view name returned by a managed bean method as follows:


<h:commandButton action=”#{loginBean.redirect}” value=”Login” />


Here the view name is obtained by calling the redirect() method of loginBean which looks like this:

package navigation; import javax.faces.bean.*;


public class LoginBean {

public String redirect() {

return “check”;



The method returns the view name “check”. So when the button is clicked, JSF forwards the request to the page check.xhtml. The bean method may take a parameter:

public String redirect(String str) { return str; }

This is referred to as:

<h:commandButton action=”#{loginBean.redirect(‘check’)}” value=”Login” />

Ability to pass a parameter to bean method gives us more control on page navigation. For example, the bean method may return different view names based on the parameter passed as follows:

public String redirect(String str) { return ”page”+str; }

We can now use the same method to navigate different pages as follows:

<h:commandButton action=”#{loginBean.redirect(‘1’)}” value=”Page1” />

<h:commandButton action=”#{loginBean.redirect(‘2’)}” value=”Page2” />

allows us to navigate a page conditionally.

3. Using Navigation Rule in Faces-config.xml

Navigation rules may be specified explicitly in faces-config.xml as follows:














This specifies a set of rules to be used to find pages from the page index.xhtml. This first rule says that for form action #{controller.method1} that outcomes page, the page p1.xhtml is to be used. Similarly, for form action #{controller.method2} that outcomes page, the page p2.xhtml is to be used. Note that the target view is determined from the form action as well as outcome. This means even if the outcome of two different form actions is same, different target views may be specified.

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *