JSF Tutorial - JSF Managed Beans








JSF Managed Bean is a regular Java Bean class registered with JSF.

The managed bean contains the getter and setter methods, business logic.

JSF Managed beans works as Model for UI component. It stores the data used by the JSF xhtml pages.

With the help of JSF framework Managed Bean can be accessed from JSF page.

In JSF 1.2, we have to register a managed bean in JSF configuration file such as faces-config.xml.

From JSF 2.0, Managed beans can be registered using annotations.

Using XML Configuration

The following code shows how to register a JSF managed bean with

<managed-bean>
  <managed-bean-name>helloWorld</managed-bean-name>
  <managed-bean-class>com.java2s.test.HelloWorld</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
</managed-bean> 
<managed-bean>
  <managed-bean-name>message</managed-bean-name>
  <managed-bean-class>com.java2s.test.Message</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
</managed-bean>




Using @ManagedBean Annotation

The following code shows how to use annotation to register a JSF managed bean.

@ManagedBean(name = "helloWorld", eager = true)
@RequestScoped
public class HelloWorld {
  
   @ManagedProperty(value="#{message}")
   private Message message;
   ...
}

@ManagedBean marks a bean as a managed bean with the name specified in name attribute.

If the name attribute is not specified, then the managed bean name will default to simple class name with first letter lowercased. In our case it would be helloWorld.

If eager is set to "true" then managed bean is created before it is requested.

"lazy" initialization is used where bean will be created only when it is requested.





Scope Annotations

Scope annotations set the scope for the managed bean.

If scope is not specified then bean will default to request scope.

We can set the JSF bean scope to as the following list.

  • @RequestScoped bean lives as long as the HTTP request-response lives. It get created upon a HTTP request and get destroyed when the HTTP response associated with the HTTP request is finished.
  • @NoneScoped bean stays as long as a single Expression Language(EL) evaluation. It get created upon an EL evaluation and get destroyed after the EL evaluation.
  • @ViewScoped bean lives as long as user is interacting with the same JSF view in the browser window. It gets created upon a HTTP request and gets destroyed when users navigate to a different view.
  • @SessionScoped bean lives as long as the HTTP session lives. It gets created upon the first HTTP request and gets destroyed when the HTTP session is invalidated.
  • @ApplicationScoped bean lives as long as the web application lives. It gets created upon the first HTTP request or when the web application starts up and the eager=true attribute is set in @ManagedBean and gets destroyed when the web application shuts down.
  • @CustomScoped bean lives as long as the bean's entry in the custom Map which is created for this scope lives.

@ManagedProperty Annotation

JSF is a simple static Dependency Injection(DI) framework. @ManagedProperty annotation marks a managed bean's property to be injected in another managed bean.