background image

Configuring a Bean

<< Converter Instance | Using the Unified EL >>
<< Converter Instance | Using the Unified EL >>

Configuring a Bean

Configuring a Bean
JavaServer Faces technology supports a sophisticated managed bean creation facility, which
allows application architects to do the following:
Configure simple beans and more complex trees of beans
Initialize bean properties with values
Place beans in a particular scope
Expose the beans to the unified EL so that page authors can access them
An application architect configures the beans in the application configuration resource file. To
learn how to configure a managed bean, see
"Configuring Beans" on page 439
. The managed
bean configuration used by the Guess Number example is the following:
<managed-bean>
<managed-bean-name>UserNumberBean</managed-bean-name>
<managed-bean-class>
guessNumber.UserNumberBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>minimum</property-name>
<property-class>long</property-class>
<value>0</value>
</managed-property>
<managed-property>
<property-name>maximum</property-name>
<property-class>long</property-class>
<value>10</value>
</managed-property>
</managed-bean>
The JavaServer Faces implementation processes this element on application startup time. When
UserNumberBean
is first referenced from the page, the JavaServer Faces implementation
initializes it and sets the values of the properties, maximum and minimum. The bean is then stored
in session scope if no instance exists. As such, the bean is available for all pages in the
application.
A page author can then access the bean properties from the component tags on the page using
the unified EL, as shown here:
<h:outputText value=
"#{UserNumberBean.minimum}"/>
The part of the expression before the . matches the name defined by the managed-bean-name
element. The part of the expression after the . matches the name defined by the property-name
element corresponding to the same managed-bean declaration.
Backing Beans
Chapter 10 · JavaServer Faces Technology
311