Javatpoint Logo
Javatpoint Logo

JSF Managed Bean

It is a pure Java class which contains set of properties and set of getter, setter methods.

Following are the common functions that managed bean methods perform:

  • Validating a component's data
  • Handling an event fired by a component
  • Performing processing to determine the next page to which the application must navigate

It also works as model for JFS Framework.

JSF Managed Bean Example

You can use this bean by the following ways.

  1. By configuring into XML file.
  2. By using annotations.

Configuring Managed Bean into XML file

This is an older approach to configure bean into xml file. In this approach, we have to create a xml file named faces-config.xml. JSF provides <managec-bean> tag to configure the bean.

In the above example, we are listing bean-name, bean-class and bean-scope. So, it can be accessible in the project.


Configuring Managed Bean using Annotations

The @ManagedBean annotation in a class automatically registers that class as a resource with the JavaServer Faces. Such a registered managed bean does not need managed-bean configuration entries in the application configuration resource file.

This is an alternative to the application configuration resource file approach and reduce the task of configuring managed beans.

The @RequestScoped annotation is used to provide scope for ManagedBean. You can use annotations to define the scope in which the bean will be stored.

You can use following scopes for a bean class:

  • Application (@ApplicationScoped): Application scope persists across all users? interactions with a web application.
  • Session (@SessionScoped): Session scope persists across multiple HTTP requests in a web application.
  • View (@ViewScoped): View scope persists during a user?s interaction with a single page (view) of a web application.
  • Request (@RequestScoped): Request scope persists during a single HTTP request in a web application.
  • None (@NoneScoped): Indicates a scope is not defined for the application.
  • Custom (@CustomScoped): A user-defined, nonstandard scope. Its value must be configured as a java.util.Map. Custom scopes are used infrequently.

Eager Managed Bean

Managed bean is lazy by default. It means, bean is instantiated only when a request is made from the application.

You can force a bean to be instantiated and placed in the application scope as soon as the application is started. You need to set eager attribute of the managed bean to true as shown in the following example:


Next TopicJSF Example





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA