To instantiate backing beans used in a JavaServer Faces application and store them in scope, you use the managed bean creation facility. This facility is configured in the application configuration resource file using
managed-beanXML elements to define each bean. This file is processed at application startup time. When a page references a bean, the JavaServer Faces implementation initializes it according to its configuration in the application configuration resource file.
With the managed bean creation facility, you can:
- Create beans in one centralized file that is available to the entire application, rather than conditionally instantiate beans throughout the application.
- Customize the bean's properties without any additional code.
- When a managed bean is created, customize the bean's property values directly from within the configuration file.
valueelements, set the property of one managed bean to be the result of evaluating another value-binding expression.
This section shows you how to initialize backing beans using the managed bean creation facility. Writing Component Properties explains how to write backing bean properties. Writing Backing Bean Methods explains how to write backing bean methods. Binding Component Values and Instances to External Data Sources explains how to reference a managed bean from the component tags.
Using the managed-bean Element
You create a backing bean using a
managed-beanelement, which represents an instance of a bean class that must exist in the application. At runtime, the JavaServer Faces implementation processes the
managed-beanelement. If a page references the bean, the JavaServer Faces implementation instantiates the bean as specified by the element configuration if no instance exists.
Here is an example managed bean configuration from the Duke's Bookstore application:<managed-bean>
<managed-bean-name> NA </managed-bean-name>
<managed-bean-scope> application </managed-bean-scope>
managed-bean-nameelement defines the key under which the bean will be stored in a scope. For a component to map to this bean, the component tag's
valueattribute must match the
managed-bean-nameup to the first period. For example, this
valueexpression maps to the
shapeproperty of the
The part before the . matches the
ImageArea. Using the HTML Component Tags has more examples of using
valueto bind components to bean properties.
managed-bean-classelement defines the fully qualified name of the JavaBeans component class used to instantiate the bean. It is the application developer's responsibility to ensure that the class complies with the configuration of the bean in the application configuration resource file. For example, the property definitions must match those configured for the bean.
managed-bean-scopeelement defines the scope in which the bean will be stored. The four acceptable scopes are
application. If you define the bean with a
nonescope, the bean is instantiated anew each time it is referenced, and so it does not get saved in any scope. One reason to use a scope of
noneis that a managed bean references another managed bean. The second bean should be in
nonescope if it is supposed to be created only when it is referenced. See Initializing Managed Bean Properties for an example of initializing a managed bean property.
If you are configuring a backing bean that is referenced by a component tag's
bindingattribute, you should define the bean with a request scope. If you placed the bean in session or application scope instead, the bean would need to take precautions to ensure thread safety because
UIComponentinstances depend on running inside of a single thread.
managed-beanelement can contain zero or more
managed-propertyelements, each corresponding to a property defined in the bean class. These elements are used to initialize the values of the bean properties. If you don't want a particular property initialized with a value when the bean is instantiated, do not include a
managed-propertydefinition for it in your application configuration resource file.
managed-beanelement does not contain other
managed-beanelements, it can contain one
map-entrieselement configures a set of beans that are instances of
list-entrieselement configures a set of beans that are instances of
To map to a property defined by a
managed-propertyelement, you must ensure that the part of a component tag's
valueexpression after the . matches the
property-nameelement. In the earlier example, the
shapeproperty is initialized with the value
poly. The next section explains in more detail how to use the
Initializing Properties using the managed-property Element
managed-propertyelement must contain a
property-nameelement, which must match the name of the corresponding property in the bean. A
managed-propertyelement must also contain one of a set of elements (listed in Table 21-1) that defines the value of the property. This value must be of the same type as that defined for the property in the corresponding bean. Which element you use to define the value depends on the type of the property defined in the bean. Table 21-1 lists all the elements used to initialize a value.
Table 21-1 Subelements of managed-property Elements That Define Property Values Element ValueThat it Defines
Defines the values in a list
Defines the values of a map
Explicitly sets the property to
Defines a single value, such as a
int, or aJavaServer Faces EL expression
Using the managed-bean Element includes an example of initializing
Stringproperties using the
valuesubelement. You also use the
valuesubelement to initialize primitive and other reference types. The rest of this section describes how to use the
valuesubelement and other subelements to initialize properties of type
Collection, as well as initialization parameters.
Referencing an Initialization Parameter
Another powerful feature of the managed bean creation facility is the ability to reference implicit objects from a managed bean property.
Suppose that you have a page that accepts data from a customer, including the customer's address. Suppose also that most of your customers live in a particular area code. You can make the area code component render this area code by saving it in an implicit object and referencing it when the page is rendered.
You can save the area code as an initial default value in the context
initParamimplicit object by adding a context parameter to your web application and setting its value using
deploytool. For example, to set a context parameter called
deploytool, open the web application, select the web application from the tree, select the Context tab, add a new context parameter, and enter
defaultAreaCodein the Coded Parameter field and
650in the Value field.
Next, you write a
managed-beandeclaration that configures a property that references the parameter:<managed-bean>
To access the area code at the time the page is rendered, refer to the property from the
Retrieving values from other implicit objects is done in a similar way. See Table 18-9 for a list of implicit objects.
Initializing Map Properties
map-entrieselement is used to initialize the values of a bean property with a type of
map-entrieselement is used within a
managed-propertyelement. Here is the definition of
web-facesconfig_1_0.dtd, located at
http://java.sun.com/dtd/web-facesconfig_1_0.dtdthat defines the application configuration resource file:
As this definition shows, a
map-entrieselement contains an optional
key-classelement, an optional
value-classelement, and zero or more
Here is the definition of
map-entryfrom the DTD:
According to this definition, each of the
map-entryelements must contain a
keyelement and either a
valueelement. Here is an example that uses the
<key>My Early Years: Growing Up on *7</key>
<key>Web Servers for Fun and Profit</key>
The map that is created from this
map-entriestag contains two entries. By default, all the keys and values are converted to
java.lang.String. If you want to specify a different type for the keys in the map, embed the
key-classelement just inside the
This declaration will convert all the keys into
java.math.BigDecimal. Of course, you must make sure that the keys can be converted to the type that you specify. The key from the example in this section cannot be converted to a
java.math.BigDecimalbecause it is a
If you also want to specify a different type for all the values in the map, include the
value-classelement after the
Note that this tag sets only the type of all the
map-entryin the preceding example includes a
valuesubelement defines a single value, which will be converted to the type specified in the bean.
valueelement, which references a property on another bean. Referencing another bean from within a bean property is useful for building a system from fine-grained objects. For example, a request-scoped form-handling object might have a pointer to an application-scoped database mapping object. Together the two can perform a form-handling task. Note that including a reference to another bean will initialize the bean if it does not already exist.
Instead of using a
map-entrieselement, it is also possible to assign the entire map using a
valueelement that specifies a map-typed expression.
Initializing Array and List Properties
valueselement is used to initialize the values of an
Listproperty. Each individual value of the array or
Listis initialized using a
null-valueelement. Here is an example:
This example initializes an
List. The type of the corresponding property in the bean determines which data structure is created. The
valueselement defines the list of values in the
valueelement specifies a single value in the
Listand can reference a property in another bean. The
null-valueelement will cause the
setBooksmethod to be called with an argument of
nullproperty cannot be specified for a property whose data type is a Java primitive, such as
Initializing Managed Bean Properties
Sometimes you might want to create a bean that also references other managed beans so that you can construct a graph or a tree of beans. For example, suppose that you want to create a bean representing a customer's information, including the mailing address and street address, each of which is also a bean. The following
managed-beandeclarations create a
CustomerBeaninstance that has two
AddressBeanproperties: one representing the mailing address, and the other representing the street address. This declaration results in a tree of beans with
CustomerBeanas its root and the two
AddressBeanobjects as children.<managed-bean>
<managed-bean-scope> request </managed-bean-scope>
<managed-bean-scope> none </managed-bean-scope>
CustomerBeandeclaration (with the
customer) creates a
CustomerBeanin request scope. This bean has two properties:
streetAddress. These properties use the
valueelement to reference a bean named
The second managed bean declaration defines an
AddressBeanbut does not create it because its
managed-bean-scopeelement defines a scope of
none. Recall that a scope of
nonemeans that the bean is created only when something else references it. Because both the
valueelement, two instances of
AddressBeanare created when
When you create an object that points to other objects, do not try to point to an object with a shorter life span because it might be impossible to recover that scope's resources when it goes away. A session-scoped object, for example, cannot point to a request-scoped object. And objects with
nonescope have no effective life span managed by the framework, so they can point only to other
nonescoped objects. Table 21-2 outlines all of the allowed connections.
Table 21-2 Allowable Connections Between Scoped Objects An Object of This Scope May Point to an Object of This Scope
You should also not allow cyclical references between objects. For example, neither of the
AddressBeanobjects in the preceding example should point back to the
CustomerBeanalready points to the
Initializing Maps and Lists
In addition to configuring
Listproperties, you can also configure a
Listdirectly so that you can reference them from a tag rather than referencing a property that wraps a
The Duke's Bookstore application configures a
Listto initialize the list of free newsletters, from which users can choose a set of newsletters to subscribe to on the
This configuration initializes a
newsletters. This list is composed of
SelectIteminstances, which are also managed beans. See The UISelectItem, UISelectItems, and UISelectItemGroup Components for more information on
SelectItem. Note that, unlike the example in Initializing Map Properties, the newsletters list is not a property on a managed bean. (It is not wrapped with a
managed-propertyelement.) Instead, the list is the managed bean.