10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Configuring Beans<br />

440<br />

Referencing a <strong>Java</strong> Enum Type<br />

As of version 1.2 of <strong>Java</strong>Server Faces technology, a managed bean property can also be a <strong>Java</strong><br />

Enum type (see http://java.sun.com/javase/6/docs/api/java/lang/Enum.html). In this<br />

case, the value element of the managed-property element must be a String that matches one<br />

of the String constants of the Enum. In other words, the String must be one of the valid values<br />

that can be returned if you were to call valueOf(Class, String) on enum, where Class is the<br />

Enum class and String is the contents of the value subelement. For example, suppose the<br />

managed bean property is the following:<br />

public enum Suit { Hearts, Spades, Diamonds, Clubs}<br />

...<br />

public Suit getSuit() { ... return Suit.Hearts; }<br />

Assuming that you want to configure this property in the application configuration file, the<br />

corresponding managed-property element would look like this:<br />

<br />

Suit<br />

Hearts<br />

<br />

When the system encounters this property, it iterates over each of the members of the enum and<br />

calls toString() on each member until it finds one that is exactly equal to the value from the<br />

value element.<br />

Referencing an Initialization Parameter<br />

Another powerful feature of the managed bean creation facility is the ability to reference<br />

implicit objects from a managed bean property.<br />

Suppose that you have a page that accepts data from a customer, including the customer’s<br />

address. Suppose also that most of your customers live in a particular area code. You can make<br />

the area code component render this area code by saving it in an implicit object and referencing<br />

it when the page is rendered.<br />

You can save the area code as an initial default value in the context initParam implicit object by<br />

adding a context parameter to your web application and setting its value in the deployment<br />

descriptor. For example, to set a context parameter called defaultAreaCode to 650, add a<br />

context-param element to the deployment descriptor, give the parameter the name<br />

defaultAreaCode and the value 650.<br />

Next, you write a managed-bean declaration that configures a property that references the<br />

parameter:<br />

<br />

customer<br />

CustomerBean<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!