JSF Managed Beans - JavaServer Faces (JSF)

What is JSF Managed Beans?

The regular Java Beans class registered with and managed by JSF is Managed Bean. Managed bean facilitates with the getter and setter methods, business logic and backing bean (a bean with HTML values).

Managed beans are accessed from JSF page and works as model for UI components.

In JSF 1.2, a managed bean had to register it in JSF configuration file such as facesconfig.xml. From JSF 2.0 onwards, managed beans can be easily registered using annotations. This approach keeps beans and its registration at one place hence it becomes easier to manage.

Using XML Configuration

Using Annotation

What is @ManagedBean Annotation?

@ManagedBean is a managed bean with a name specified in the name attribute. Managed bean name will default to class name portion of the qualified class name, if the name attribute is not specified. In the tutorial class it is helloWorld.

Another important attribute is eager. If eager = "true" then managed bean is created before it is requested for the first time otherwise "lazy" initialization is used in which bean will be created only when it is requested.

What is scope Annotation?

Scope annotation set the scope into which managed bean is placed. Bean will default to request scope in case scope is not specified. The following are the list of scopes:

Scope & Description
Bean lives as long as the HTTP request-response lives. It gets created upon a HTTP request and gets destroyed when the HTTP response associated with the HTTP request is finished.
Bean lives as long as a single EL evaluation. It gets created upon an EL evaluation and gets destroyed immediately after the EL evaluation.
Bean lives as long as the user is interacting with the same JSF view in the browser window/tab. It gets created upon a HTTP request and gets destroyed once the user postbacks to a different view.
Bean lives as long as the HTTP session lives. It gets created upon the first HTTP request involving this bean in the session and gets destroyed when the HTTP session is invalidated.
Bean lives as long as the web application lives. It gets created upon the first HTTP request involving this bean in the application (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.
Bean lives as long as the bean's entry in the custom Map, which is created for this scope lives.

What is @ManagedProperty Annotation?

A property of the managed bean can be injected into another managed bean using @ManagedProperty annotation.

For instance,

A JSF application is created to test the above annotations for the managed beans.

Create a project with a namehelloworldunder a packagecom.wisdomjobs.testas explained in theJSF - Create Applicationchapter.
ModifyHelloWorld.javaas explained below. Keep the rest of the files unchanged.
CreateMessage.javaunder a packagecom.wisdomjobs.testas explained below.
Compile and run the application to make sure business logic is working as per the requirements.
Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver.
Launch your web application using appropriate URL as explained below in the last step.




Once all the changes are done, the application is compiled and run as in JSF – Create Application chapter. On successful creation of the application, the result appears as:

JSF Application Result

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JavaServer Faces (JSF) Topics