Java Reference
In-Depth Information
public enum Suit { Hearts, Spades, Diamonds, Clubs}
...
public Suit getSuit() { ... return Suit.Hearts; }
Assuming you want to configure this property in the application configuration resource
file, the corresponding
managed-property
element looks 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 a Context Initialization Parameter
Another powerful feature of the managed bean creation facility is the ability to reference
implicit objects from a managed bean property.
Suppose you have a page that accepts data from a customer, including the customer's ad-
dress. 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
de-
faultAreaCode
to
650
, add a
context-param
element to the deployment
descriptor, and 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>