Wednesday, June 11, 2008

JSF Managed Beans

Managed beans are any JavaBeans used by the application that are registered in the JSF faces-config.xml file. When the JSF application starts up, it parses this configuration file and the beans are made available. Whenever a managed bean is referenced (for example in an EL expression as a value for a component's tag attribute - called a value binding), the Managed Bean Creation Facility instantiates the bean by calling the default constructor method on the bean. If any properties are also declared, they are populated with the declared default values.

For example:
<managed-bean>
<managed-bean-name>contactController</managed-bean-name>
<managed-bean-class>
example.jsf.controller.ContactController
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

managed-bean-scope
This determines the scope within which the bean is stored. The following are the valid scopes for a bean.
  • Application:The bean is available for the duration of the web application. This is helpful for global beans such as LDAP directories.

  • Session: The bean is available to the client throughout the client's session.

  • Request: The bean is available from the time it is instantiated until a response is sent back to the client. This is usually the life of the current page.

  • None: The bean is instantiated each time it is referenced. This is helpful if the bean is referenced within another bean.


Get the session level bean instance in code
configuration
<managed-bean>
<managed-bean-name>user</managed-bean-name>
<managed-bean-class>
example.jsf.UserBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

code
FacesContext context = FacesContext.getCurrentInstance();
ValueBinding binding =
context.getApplication().createValueBinding("#{user}");
UserBean user = (UserBean) binding.getValue(context);


Get the application level bean instance in code
configuration
<managed-bean>
<managed-bean-name>groupService</managed-bean-name>
<managed-bean-class>
example.jsf.model.service.GroupService
</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>

code
GroupService groupService =
(GroupService) facesContext
.getExternalContext().getApplicationMap()
.get("groupService");


Lists and Maps
Managed beans and managed bean properties can be also be initialized as lists or maps, provided that the bean or property type is a List or Map or implements java.util.Map or java.util.List. The default type for the values within a list or map is java.lang.String, unless another class is declared using the <value-class> or <key-class> element.

For lists, the <list-entries> element works as a container for each value within the list, as shown in the following example. Note that this managed bean is a list.
<managed-bean>
<managed-bean-name>options</managed-bean-name>
<managed-bean-class>java.util.ArrayList</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
<list-entries>
<value>Text Only</value>
<value>Text + HTML</value>
<value>HTML Only</value>
</list-entries>
</managed-bean>

For maps, individual <map-entry> elements define the key/value pairs within the map, as shown in the following example. Note that in this example, the managed property is the map and that because the key is an integer, the <key-class> element must be declared.
<managed-bean>
<managed-bean-name>errorcodes</managed-bean-name>
<managed-bean-class>com.jsf.databeans.ErrorCodes</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
<managed-property>
<property-name>error</property-name>
<property-class>java.lang.String</property-class>
<map-entries>
<key-class>java.lang.Integer</key-class>
<map-entry>
<key>300</key>
<value>Invalid entry</value>
</map-entry>
<map-entry>
<key>400</key>
<value>No such value</value>
</map-entry>
<map-entry>
<key>500</key>
<value>Internal server error</value>
</map-entry>
</map-entries>
</managed-property>
</managed-bean>

No comments:

Post a Comment