English 中文(简体)
JSF - Managed Beans
  • 时间:2024-12-22

JSF - Managed Beans


Previous Page Next Page  

Managed Bean is a regular Java Bean class registered with JSF. In other words, Managed Beans is a Java bean managed by JSF framework. Managed bean contains the getter and setter methods, business logic, or even a backing bean (a bean contains all the HTML form value).

Managed beans works as Model for UI component. Managed Bean can be accessed from JSF page.

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

<managed-bean>
   <managed-bean-name>helloWorld</managed-bean-name>
   <managed-bean-class>com.tutorialspoint.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.tutorialspoint.test.Message</managed-bean-class>
   <managed-bean-scope>request</managed-bean-scope>
</managed-bean> 

Using Annotation

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

@ManagedBean Annotation

@ManagedBean marks a bean to be 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 class name portion of the fully quapfied class name. In our case, it would be helloWorld.

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

Scope Annotations

Scope annotations set the scope into which the managed bean will be placed. If the scope is not specified, then bean will default to request scope. Each scope is briefly discussed in the following table.

S.No Scope & Description
1

@RequestScoped

Bean pves as long as the HTTP request-response pves. It gets created upon a HTTP request and gets destroyed when the HTTP response associated with the HTTP request is finished.

2

@NoneScoped

Bean pves as long as a single EL evaluation. It gets created upon an EL evaluation and gets destroyed immediately after the EL evaluation.

3

@ViewScoped

Bean pves 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.

4

@SessionScoped

Bean pves as long as the HTTP session pves. It gets created upon the first HTTP request involving this bean in the session and gets destroyed when the HTTP session is invapdated.

5

@ApppcationScoped

Bean pves as long as the web apppcation pves. It gets created upon the first HTTP request involving this bean in the apppcation (or when the web apppcation starts up and the eager=true attribute is set in @ManagedBean) and gets destroyed when the web apppcation shuts down.

6

@CustomScoped

Bean pves as long as the bean s entry in the custom Map, which is created for this scope pves.

@ManagedProperty Annotation

JSF is a simple static Dependency Injection (DI) framework. Using @ManagedProperty annotation, a managed bean s property can be injected in another managed bean.

Example Apppcation

Let us create a test JSF apppcation to test the above annotations for managed beans.

Step Description
1 Create a project with a name helloworld under a package com.tutorialspoint.test as explained in the JSF - Create Apppcation chapter.
2 Modify HelloWorld.java as explained below. Keep the rest of the files unchanged.
3 Create Message.java under a package com.tutorialspoint.test as explained below.
4 Compile and run the apppcation to make sure business logic is working as per the requirements.
5 Finally, build the apppcation in the form of war file and deploy it in Apache Tomcat Webserver.
6 Launch your web apppcation using appropriate URL as explained below in the last step.

HelloWorld.java

package com.tutorialspoint.test;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

@ManagedBean(name = "helloWorld", eager = true)
@RequestScoped
pubpc class HelloWorld {
   @ManagedProperty(value = "#{message}")
   private Message messageBean;
   private String message;
   
   pubpc HelloWorld() {
      System.out.println("HelloWorld started!");   
   }
   
   pubpc String getMessage() {
      
      if(messageBean != null) {
         message = messageBean.getMessage();
      }       
      return message;
   }
   
   pubpc void setMessageBean(Message message) {
      this.messageBean = message;
   }
}

Message.java

package com.tutorialspoint.test;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name = "message", eager = true)
@RequestScoped
pubpc class Message {
   private String message = "Hello World!";
	
   pubpc String getMessage() {
      return message;
   }
   pubpc void setMessage(String message) {
      this.message = message;
   }
}

home.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">
   <head>
      <title>JSF Tutorial!</title>
   </head>
   
   <body>
      #{helloWorld.message}
   </body>
</html>

Once you are ready with all the changes done, let us compile and run the apppcation as we did in JSF - Create Apppcation chapter. If everything is fine with your apppcation, this will produce the following result.

JSF Managed Beans Advertisements