Managed BeansManaged 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>
<managed-bean-name>helloWorld</managed-bean-name> <managed-bean-class>com.javatechnologycenter.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.javatechnologycenter.test.Message</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> Using Annotation
@ManagedBean(name = "helloWorld", eager = true)
@RequestScoped public 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 qualified 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" initialization 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 scope is not specified then bean will default to request scope. Each scope is briefly discussed below
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 Application Let us create a test JSF application to test the above annotations for managed beans.
package com.javatechnologycenter.test;
import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; import javax.faces.bean.RequestScoped; @ManagedBean(name = "helloWorld", eager = true) @RequestScoped public class HelloWorld { @ManagedProperty(value="#{message}") private Message messageBean; private String message; public HelloWorld() { System.out.println("HelloWorld started!"); } public String getMessage() { if(messageBean != null){ message = messageBean.getMessage(); } return message; } public void setMessageBean(Message message) { this.messageBean = message; } } Message.java
package com.javatechnologycenter.test;
import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; @ManagedBean(name = "message", eager = true) @RequestScoped public class Message { private String message = "Hello World!"; public String getMessage() { return message; } public 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 application as we did in JSF - Create Application chapter. If everything is fine with your application, this will produce following result: |