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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>The</strong> web container evaluates a variable that appears in an expression by looking up its value<br />

according to the behavior of PageContext.findAttribute(String), where the String<br />

argument is the name of the variable. For example, when evaluating the expression<br />

${customer}, the container will look for customer in the page, request, session, and application<br />

scopes and will return its value. If customer is not found, null is returned. A variable that<br />

matches one of the implicit objects described in “Implicit Objects” on page 161 will return that<br />

implicit object instead of the variable’s value.<br />

You can alter the way variables are resolved with a custom EL resolver, which is a new feature of<br />

the unified EL. For instance, you can provide an EL resolver that intercepts objects with the<br />

name customer, so that ${customer} returns a value in the EL resolver instead. However, you<br />

cannot override implicit objects in this way. See “EL Resolvers” on page 159 for more<br />

information on EL resolvers.<br />

You can set the variable name, customer, when you declare the bean. See “Creating and Using a<br />

<strong>Java</strong>Beans Component” on page 167 for information on how to declare a <strong>Java</strong>Beans component<br />

for use in your JSP pages.<br />

To declare beans in <strong>Java</strong>Server Faces applications, you use the managed bean facility. See<br />

“Backing Beans” on page 307 for information on how to declare beans for use in <strong>Java</strong>Server<br />

Faces applications.<br />

When referencing an enum constant with an expression, you use a String literal. For example,<br />

consider this Enum class:<br />

public enum Suit {hearts, spades, diamonds, clubs}<br />

To refer to the Suit constant, Suit.hearts with an expression, you use the String literal,<br />

"hearts". Depending on the context, the String literal is converted to the enum constant<br />

automatically. For example, in the following expression in which mySuit is an instance of Suit,<br />

"hearts" is first converted to a Suit.hearts before it is compared to the instance.<br />

${mySuit == "hearts"}<br />

Referring to Object Properties UsingValue Expressions<br />

Unified Expression Language<br />

To refer to properties of a bean or an Enum instance, items of a collection, or attributes of an<br />

implicit object, you use the . or [] notation, which is similar to the notation used by<br />

ECMAScript.<br />

So, if you wanted to reference the name property of the customer bean, you could use either the<br />

expression ${customer.name} or the expression ${customer["name"]}. <strong>The</strong> part inside the<br />

square brackets is a String literal that is the name of the property to reference.<br />

You can use double or single quotes for the String literal. You can also combine the [] and .<br />

notations, as shown here:<br />

Chapter 5 • <strong>Java</strong>Server PagesTechnology 151

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

Saved successfully!

Ooh no, something went wrong!