background image

Referencing a Java Enum Type

<< Initializing Properties | Initializing Map Properties >>
<< Initializing Properties | Initializing Map Properties >>

Referencing a Java Enum Type

TABLE 14­1
Subelements of managed-property Elements That Define Property Values
Element
Value That It Defines
list-entries
Defines the values in a list
map-entries
Defines the values of a map
null-value
Explicitly sets the property to null
value
Defines a single value, such as a String or int, or a JavaServer Faces EL expression
"Using the managed-bean Element" on page 439
includes an example of initializing String
properties using the value subelement. You also use the value subelement to initialize
primitive and other reference types. The rest of this section describes how to use the value
subelement and other subelements to initialize properties of Java Enum types, java.util.Map,
array
, and Collection, as well as initialization parameters.
Referencing a Java Enum Type
As of version 1.2 of JavaServer Faces technology, a managed bean property can also be a Java
Enum
type (see
http://java.sun.com/javase/6/docs/api/java/lang/Enum.html
). In this
case, the value element of the managed-property element must be a String that matches one
of the String constants of the Enum. In other words, the String must be one of the valid values
that can be returned if you were to call valueOf(Class, String) on enum, where Class is the
Enum
class and String is the contents of the value subelement. For example, suppose the
managed bean property is the following:
public enum Suit { Hearts, Spades, Diamonds, Clubs}
...
public Suit getSuit() { ... return Suit.Hearts; }
Assuming that you want to configure this property in the application configuration file, the
corresponding managed-property element would look like this:
<managed-property>
<property-name>Suit</property-name>
<value>Hearts</value>
</managed-property>
When the system encounters this property, it iterates over each of the members of the enum and
calls toString() on each member until it finds one that is exactly equal to the value from the
value
element.
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.
Configuring Beans
The Java EE 5 Tutorial · September 2007
442