Saturday, July 14, 2012


JSF 2.0: Annotations, New Navigation Eliminate XML Configuration

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 thejavax.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:

        userBean
        com.demo.bean.UserBean
        session

The UserBean is rewritten using these annotations:
package com.demo.bean;
//import declarations
@ManagedBean
@SessionScoped
public class UserBean {
//code
}

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:

Alternatively, the name can also be specified using the @ManagedBean annotation as shown below:
package com.demo.bean;
//imports
@ManagedBean(name="regBean")
@RequestScoped
public class RegisterBean {
//code
}

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

Note that faces-config.xml can also be used. The runtime will consider annotations only if thefaces-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.

No comments: