January 21, 2021
Hot Topics:

JSF 2.0: Annotations, New Navigation Eliminate XML Configuration

  • By Sangeetha S, Nitin KL, Ananya S
  • Send Email »
  • More Articles »

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="#{userBean.email}"  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="#{regBean.email}"  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.

Page 1 of 2

This article was originally published on March 3, 2010

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date