×
☰ See All Chapters

JSF @ManagedBean Annotation

The @ManagedBean (javax.faces.bean.ManagedBean) annotation in a class automatically registers that class as a resource with the JavaServer Faces implementation. Such a registered managed bean does not need managed-bean configuration entries in the application faces-config.xml resource file. A class annotated with this annotation must have a public zero-argument constructor. If such a constructor is not defined on the class, a FacesException will be thrown.

Below is an example of using the @ManagedBean annotation:

package com.java4coding;

 

import javax.faces.bean.ManagedBean;

import javax.faces.bean.RequestScoped;

 

@ManagedBean

@RequestScoped

public class HelloBean {

        private String name;

        private String password;

        // getter and setter methods

        // action controller method

}

 

ManagedBean annotation has two attributes name and eager.

name: The value of the name attribute is taken to be the managed-bean-name. If the value of the name attribute is unspecified or is the empty String, the name of the managed bean is derived from taking the unqualified class name portion of the fully qualified class name and converting the first character to lower case. For example, if the ManagedBean annotation is on a class with the fully qualified class name com.java4coding.HelloBean, then helloBean is considered as the name of the bean.

Annotation

facelets

@ManagedBean

public class HelloBean {

}

<h:inputText id="nme" value="#{helloBean.name}"/>

@ManagedBean(name=”helloBean”)

public class HelloBean {

}

<h:inputText id="nme" value="#{helloBean.name}"/>

@ManagedBean(name=”hb”)

public class HelloBean {

}

<h:inputText id="nme" value="#{hb.name}"/>

@ManagedBean(name=”Bean”)

public class HelloBean {

}

<h:inputText id="nme" value="#{Bean.name}"/>

@ManagedBean(name=”UserBean”)

public class HelloBean {

}

<h:inputText id="nme" value="#{UserBean.name}"/>

 

eager: If the value of the eager attribute is true, and the scope of the managed bean is specified as "application", the runtime must instantiate this class when the application starts. This instantiation and storing of the instance must happen before any requests are serviced. If eager is unspecified or false, or the scope of managed bean is something other than "application", the default "lazy" instantiation and scoped storage of the managed bean happens.

Scope of the managed bean

The scope of the managed bean is declared using one of NoneScoped, RequestScoped, ViewScoped, SessionScoped, ApplicationScoped, or CustomScoped annotations. If the scope annotations are omitted, the bean must be handled as if the RequestScoped annotation is present.

@RequestScoped: A bean in this scope lives till response is given back to a request. Bean will be created when HTTP requets is received and destoryed after giving HTTP response.

@ViewScoped: A bean in this scoped has life as long as user is interacting with same JSF view in the browser.  Bean is created when HTTP requets is received and bean gets destoryed when user navigates to different view or facelet.

@SessionScoped: A Bean in this scope has life throught one user session. Bean is created when HTTP requets is received and bean gets destroyed when session object is destoryed or session is invalidated.

@NoneScoped: A bean has scope till an expression (EL) is evauated on view. Bean creates during expression evalution and destroyed immediated evaluation is completed.

@CustomScoped: A bean in this scope lives as long as the beans’s entry in the custom Map which is created for this scope lives. This custom map should be defined by developers.

Handling bean life cycle

You can handle the beans life cycle using below annotations:

@PostConstruct

The javax.annotation.PostConstruct annotation is used on a method that needs to be executed after bean creation. Only one method can be annotated with this annotation.

@PreDestroy

The javax.annotation.PreDestroy annotation is used on methods as a callback notification to signal that the instance is in the process of being removed by the container. The method annotated with PreDestroy is typically used to release resources that it has been holding.

Life Cycle methods criteria

The method on which the PostConstruct or PreDestroy annotation is applied must fulfill all of the following criteria.

  • Method must not have any parameter.  

  • Return type of the method must be void.  

  • Method must not throw a checked exception. 

  • Method may be public, protected, package private or private.  

  • Method must not be static except for the application client.  

  • Method may be final.  

  • If the method throws an unchecked exception the class must not be put into. 


All Chapters
Author