Managed Bean in JavaServer Faces (JSF)

Managed Beans

 
A Managed Bean, is a Java class file to which components of JSF pages are mapped.  A JSF application has one, or more, Managed Beans that handle the business logic or, a backing Bean for the values of components, displayed on the JSF page.  Normally, a Managed Bean has getter and setter methods of the properties mapped as the component's value on JSF pages.
  
A Managed Bean is used to:
  1. Validate the components value.
  2. Handle an event fired by component on UI page.

Scope of Managed Bean

 
There are primarily 4 scopes in which a Managed Bean can be instantiated.  They are:
  1. None Scoped
  2. Request Scoped
  3. Session Scoped
  4. Application Scoped
Let's take a simple example to better understand the scope of Managed Beans.
  
Example
I have created an index.xhtml file with a TextBox and a submit button.  The TextBox field value is mapped to the Bean property textname, using the expression language.  An action to call an output.xhtml page, on the click event, is defined on the command button.  The Output.xhtml file displays the value of the Bean property textname (in other words the value entered into the TextBox).
 
The following is the code for index.xhtml:
 
Code for index
 
The following is the screenshot of the index.xhtml file when run in a browser:
 
Screenshot of index
 
The following is the code for output.xhtml:
 
Code for output
A Java class named ExampleBean.java will act as a Managed Bean for this JSP page.
 
The following is the code for the ExampleBean.java file:
 
Code for ExampleBean
 
1. None Scope: A Managed Bean with this scope is not stored anywhere.  It is created whenever it is needed.
 
Let me show you the assigning of the Managed Bean, of a None scope.
 
None Scope
 
Now, when you run the index.xhtml file, enter the value into the TextBox and click on the submit button.  You will find it in the Bean, displayed multiple times, in the server output window.
 
run the index
 
The reason for multiple occurrences, in the Bean output, is the none scope of the Managed Bean.  The Managed Bean is initialized each time, it is needed.
 
2. Request Scope: Under this scope, the Managed Bean value is stored and is available, while the request is alive.
 
Request Scope
 
Now, when you run the index.xhtml file, enter the value into the TextBox and click on the submit button.  You will find it in the Bean displayed in the server output window and the output.xhtml file displayed in the browser.
 
Index.xhtml file
 
output
 
Server Output
 
Server Output
 
Output.xhtml
 
Output xhtml
 
Now, if I return to the index.xhtml file, using the browser's back button, and again enter the value into the TextBox.  Click on the submit button and then one more Managed Bean object, will be created.
 
Server Output
 
submit button
 
So, each time a request is sent to a Managed Bean, a new object will be created and saved under request scope.
 
3. Session Scope: Under this scope, the Managed Bean object is saved and is available for use in the entire session.  All of the requests, within the same session, share one instance of the Managed Bean.
 
The following is the updated code for the Managed Bean:
 
Session Scope
 
Now, when you run the index.xhtml file, the Bean class object will be created once and saved for the entire session.
 
Server Output:
 
session
 
Now, if you try and send multiple requests by going to index.xhtml and enter the value and click the submit button, the Bean class object won't be created.  There will be output from the setter method only.
 
Server Output
 
Application Scope
 
4. Application Scope: Under this scope, the Managed Bean object is saved and is available for use in the entire application, for all the sessions and requests.  A Bean is given application scope, only if we want one instance of the Bean to be shared in the application.
 
The following is the updated code for the Managed Bean:
 
Managed Bean
 
Now, when you run the index.xhtml file, the Bean class object will be created once and saved for the entire application.
 
Server Output:
 
application
 
There are two more scopes for the Managed Bean added to JSF version 2.0:
  1. View Scope: A Bean remains in view scope while the same JSF page is redisplayed.  As soon as the user navigates to a different page, the Bean goes out of scope.
     
  2. Custom Scope: In this scope, we make an entry of our Bean to a custom map and assign that map a scope (normally a Session Scope).  A Bean lives as long as the scope of the custom map lives.
I hope this article was useful to you.  Thank you for reading it.