background image

The map-entries Tag

<< Initializing Map Properties | Initializing Array and List Properties >>
<< Initializing Map Properties | Initializing Array and List Properties >>

The map-entries Tag

<map-entries>
<map-entry>
<key>My Early Years: Growing Up on *7</key>
<value>30.75</value>
</map-entry>
<map-entry>
<key>Web Servers for Fun and Profit</key>
<value>40.75</value>
</map-entry>
</map-entries>
</managed-property>
</managed-bean>
The map that is created from this map-entries tag 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-class element just inside the map-entries element:
<map-entries>
<key-class>java.math.BigDecimal</key-class>
...
</map-entries>
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.BigDecimal because it is a String.
If you also want to specify a different type for all the values in the map, include the value-class
element after the key-class element:
<map-entries>
<key-class>int</key-class>
<value-class>java.math.BigDecimal</value-class>
...
</map-entries>
Note that this tag sets only the type of all the value subelements.
The first map-entry in the preceding example includes a value subelement. The value
subelement defines a single value, which will be converted to the type specified in the bean.
The second map-entry defines a value element, 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-entries element, it is also possible to assign the entire map using a
value
element that specifies a map-typed expression.
Configuring Beans
The Java EE 5 Tutorial · September 2007
444