background image

Initializing Map Properties

<< Referencing a Java Enum Type | The map-entries Tag >>
<< Referencing a Java Enum Type | The map-entries Tag >>

Initializing Map Properties

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 initParam implicit object by
adding a context parameter to your web application and setting its value in the deployment
descriptor. For example, to set a context parameter called defaultAreaCode to 650, add a
context-param
element to the deployment descriptor, give the parameter the name
defaultAreaCode
and the value 650.
Next, you write a managed-bean declaration that configures a property that references the
parameter:
<managed-bean>
<managed-bean-name>customer</managed-bean-name>
<managed-bean-class>CustomerBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>areaCode</property-name>
<value>#{initParam.defaultAreaCode}</value>
</managed-property>
...
</managed-bean>
To access the area code at the time the page is rendered, refer to the property from the area
component tag's value attribute:
<h:inputText id=area value=
"#{customer.areaCode}"
Retrieving values from other implicit objects is done in a similar way. See
"Implicit Objects" on
page 162
for a list of implicit objects.
Initializing Map Properties
The map-entries element is used to initialize the values of a bean property with a type of
java.util.Map
if the map-entries element is used within a managed-property element. A
map-entries
element contains an optional key-class element, an optional value-class
element, and zero or more map-entry elements.
Each of the map-entry elements must contain a key element and either a null-value or value
element. Here is an example that uses the map-entries element:
<managed-bean>
...
<managed-property>
<property-name>prices</property-name>
Configuring Beans
Chapter 14 · Configuring JavaServer Faces Applications
443