background image

Initializing Properties

<< Using the managed-bean Element | Referencing a Java Enum Type >>
<< Using the managed-bean Element | Referencing a Java Enum Type >>

Initializing Properties

the class complies with the configuration of the bean in the application configuration resource
file. This includes making sure that the types of the properties in the bean match those
configured for the bean in the configuration file.
The managed-bean-scope element defines the scope in which the bean will be stored. The four
acceptable scopes are none, request, session, or application. If you define the bean with a
none
scope, 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 none is that a managed bean references another
managed bean. The second bean should be in none scope if it is supposed to be created only
when it is referenced. See
"Initializing Managed Bean Properties" on page 445
for an example of
initializing a managed bean property.
If you are configuring a backing bean that is referenced by a component tag's binding attribute,
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
UIComponent
instances depend on running inside of a single thread.
The managed-bean element can contain zero or more managed-property elements, 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-property definition for it in your
application configuration resource file.
If a managed-bean element does not contain other managed-bean elements, it can contain one
map-entries
element or list-entries element. The map-entries element configures a set of
beans that are instances of Map. The list-entries element configures a set of beans that are
instances of List.
To map to a property defined by a managed-property element, you must ensure that the part of
a component tag's value expression after the period matches the managed-property element's
property-name
element. In the earlier example, the shape property is initialized with the value
poly
. The next section explains in more detail how to use the managed-property element.
Initializing Properties Using the managed-property
Element
A managed-property element must contain a property-name element, which must match the
name of the corresponding property in the bean. A managed-property element must also
contain one of a set of elements (listed in
Table 14­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 14­1
lists all the elements used to initialize a value.
Configuring Beans
Chapter 14 · Configuring JavaServer Faces Applications
441