Archives for 

Java Server Faces

This section describes about Java Server Faces

Navigation rules in JSF

In the previous chapter we have discussed the fundamentals of JSF. We also made the basic set up to develop a JSF application.In this chapter we are looking in to another concept – navigation rules in JSF

Navigation rules in JSF

In JSF , developers can mention the page navigation  in multiple ways  . At run time ,page  navigation happens based on  these navigation rules. So these navigation rules decides which view needs to be shown based on an outcome.

There are multiple ways to define the navigation rules.They are:

1)Implicit Navigation:-In JSF 2.0 , developer can specify the navigation rule in  the page itself.The outcome view name can be specified as the action in the JSF page. JSF will  search for the correct view from the list of deployed views and will be displayed if found.(As in the previous example)

2)Navigation Rule in Managed Bean : In this case  the  managed bean action methods returns the view name . JSF will search for the correct view among all the deployed list of views and will be shown if found.

3)Navigation rule in faces-config.xml file :-In faces-config file , we can define the navigation rule for each action and for each outcome.

Example

Now we can look into an example.We have a login page and a success page. Once the user enters both the credentials(user name & password) and press the login button , success page will be shown. Otherwise the login page will be shown again.We are defining our navigation rules in faces-config.xml file.We  are using the same setup made for the previous example. Source file locations are also same .

We have two managed beans in this example.We have a LoginController.java which  has a method to handle the login request from the page.We also have UserBean.java ,which holds the user reference.The LoginController has a reference of the UserBean.java for holding the user object.

So it is possible to refer a managed bean from another managed bean as a managed property.To set a bean as a managed property of another bean , the child should have a scope which is equal to or less than the scope of the parent bean.Also the parent bean should have a setter of the child bean.

loginPage.xhtml

This is the source page.It has the text widgets to enter the user name and password.It also has the command button to trigger the action

LoginController.java

It has the action method.It returns either success or failure responses based on the user input.

UserBean.java

It holds the user object

faces-config.xml

It has the navigation rules for both the outcome.For success outcome from manged bean , the result view is successPage and for failure outcome , the result will be loginPage

loginSuccess.xhtml

Now build the project and run the loginPage.xhtml. A successful login will render the success view. Otherwise the login page will be shown again.

See Related Topics

1)JSF Overview