JavaWeb-based JavaJSF 2.0: Annotations, New Navigation Eliminate XML Configuration

JSF 2.0: Annotations, New Navigation Eliminate XML Configuration content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

JavaServer Faces 2.0 is a major release for the specification. This latest version of the Java component UI framework for building dynamic pages, which will be part of the Java Enterprise Edition 6 platform, has several interesting features that make development and deployment of JSF applications simple and easy.

At the highest level, JSF simplifies the web developer’s life by providing the following:

  • Reusable UI components for easy authoring of web pages
  • Well defined and simple transfer of application data to and from the UI
  • Easy state management across server requests
  • Simplified event handling model
  • Easy creation of custom UI components

Two of the most notable changes in JSF 2.0 are the introduction of annotations and the new convention used for navigation. These new features essentially make the faces-config.xml file optional. With JSF 2.0, you can use annotations in managed beans, registering listeners, resource rendering, etc. Now, any annotated POJO can be used as a managed bean.

Navigation in the new release has been completely redefined to make it simpler. With the new navigation model, the developer need not struggle with managing cumbersome XML configuration for navigation. From JSF 2.0, navigations can be implicit or conditional. In implicit navigation, the navigation happens to a view corresponding to the result of an action. In conditional navigation, a pre-condition needs to be met before navigation is enabled.

In this article, we detail the new annotations and navigation features in JSF 2.0 with some code samples.

Annotations in Managed Beans

Annotations have been introduced in JSF 2.0 to allow you to mark a POJO as a managed bean so that the class becomes a managed bean during runtime. Annotations have replaced XML entries in the faces-config.xml file. Managed beans are identified using the @ManagedBean annotation. The scope of the bean is also specified using annotations (@RequestScoped, @SessionScoped, @ApplicationScoped). However, you cannot define the managed beans in View Scope using annotations. These annotations are part of the javax.faces.bean package.

Let us first review the how you would declare a managed bean in JSF 1.x. The online quiz application uses a UserBean to represent a registered user. This has to be declared in faces-config.xml as follows:


The UserBean is rewritten using these annotations:

package com.demo.bean;
//import declarations
public class UserBean {

By default, the name of the managed bean will be the name of the annotated class, with the first letter of the class in lowercase. The UserBean managed bean can be referred to in a Facelets page as shown:

<h:inputText label="eMailID"  id="emailId" 
value="#{}"  size="20"   required="true"/>

Alternatively, the name can also be specified using the @ManagedBean annotation as shown below:

package com.demo.bean;
public class RegisterBean {

A managed bean that is used when registering new users is declared. This bean will be referred to in the Facelets page using the name mentioned in the @ManagedBean annotation as shown below:

<h:inputText label="eMailID"  id="emailId" 
value="#{}"  size="20"   required="true"/>

Note that faces-config.xml can also be used. The runtime will consider annotations only if the faces-config.xml is not there or the metadata-complete attribute of the faces-config.xml file is not set to true.

Support for annotations has been introduced for composite components, converters, validators, and renderers in lieu of manipulating the faces-config.xml file.

Navigation Using Convention Rather than Configuration

Navigation in JSF defines the set of rules for choosing the next view to be displayed after a specified action is completed. In JSF 1.x, navigation was generally specified using the faces-config.xml file.

Consider the following scenario from the online quiz application: When a user completes a question and wants to move to the next question, he/she has to click on the “Next” link that is rendered as shown below:

<h:commandLink value="Next" action="question2"/>

When this link is selected, navigation should proceed from question1.jsp to question2.jsp (assume that views are defined using JSP). This navigation rule should be defined in faces-config.xml as follows:


JSF 2.0 introduces two types of navigation to eliminate or reduce entries in the faces-config.xml file:

  • Implicit Navigation
  • Conditional or User Defined Navigation

Implicit Navigation

Implicit Navigation requires no entry in the configuration file. It allows the default navigation handler to choose the XHTML page with the matching name as specified in the action attribute of the UIComponent.

Suppose navigation from question1.xhtml to question2.xhtml (using Facelets as the view technology) should occur on the click of a link.

<h:commandLink value="Next" action="question2"/>

With implicit navigation, the value of the action attribute is treated as the to-view-id. The default navigation handler appends a ‘/‘ and .xhtml extension, and navigation proceeds from question1.xhtml to question2.xhtml. In case a bean method is invoked as part of the action, the String values returned by the method are also treated the same way.

Implicit navigation simplifies the navigation model greatly when compared with pre-JSF 2.0, as there is no need to define the navigation rules explicitly.

Conditional Navigation

Conditional navigation enables you to set a pre-condition for navigation. This pre-condition needs to be met before allowing navigation to the specific view specified in the navigation case in faces-config.xml. The pre-condition is specified using an EL expression and the &tl;if> element:


The <if> tag in the navigation case ensures that the navigation proceeds to order.xhtml only if the present user is an administrator.


In this article, we discussed the two exciting new features of the JSF 2.0 specification: the introduction of annotations and the new navigation convention. Conventional navigation replaces configuration, and annotations in managed beans makes development really easy. JSF 2.0 will definitely be a more developer-friendly framework.

Stay tuned as we will explore more notable new features in JSF 2.0 in upcoming articles.


The authors would like to sincerely thank Mr. Subrahmanya (SV, VP, ECOM Research Group, E&R) for his ideas, guidance, support and constant encouragement and Ms. Yuvarani Meiyappan (Lead, E&R) for kindly reviewing this article and for her valuable comments.

About the Authors

Sangeetha S. works as a Senior Technical Architect at the E-Commerce Research Labs at Infosys Technologies. She has over 10 years of experience in design and development of Java and Java EE applications. She has co-authored a book on ‘J2EE Architecture’ and also has written articles for online Java publications.

Nitin KL works at the E-Commerce Research Labs at Infosys Technologies. He is involved in design and development of Java EE applications using Hibernate, iBATIS, and JPA.

Ananya S. works at the E-Commerce Research Labs at Infosys Technologies. She is involved in design and development of Java EE applications using Hibernate, iBATIS, and JPA.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories