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.

Unified Expression Language<br />

154<br />

■ With text only:<br />

<br />

This expression is called a literal expression. In this case, the attribute’s String value is<br />

coerced to the attribute’s expected type. Literal value expressions have special syntax rules.<br />

See “Literal Expressions” on page 158 for more information. When a tag attribute has an<br />

enum type, the expression that the attribute uses must be a literal expression. For example,<br />

the tag attribute can use the expression "hearts" to mean Suit.hearts. <strong>The</strong> literal is<br />

coerced to Suit and the attribute gets the value Suit.hearts.<br />

All expressions used to set attribute values are evaluated in the context of an expected type. If<br />

the result of the expression evaluation does not match the expected type exactly, a type<br />

conversion will be performed. For example, the expression ${1.2E4} provided as the value of<br />

an attribute of type float will result in the following conversion:<br />

Float.valueOf("1.2E4").floatValue()<br />

See section 1.18 of the <strong>Java</strong>Server Pages 2.1 Expression Language Specification (available from<br />

http://jcp.org/about<strong>Java</strong>/communityprocess/final/jsr245/) for the complete type<br />

conversion rules.<br />

Method Expressions<br />

Another feature of the unified expression language is its support of deferred method<br />

expressions. A method expression is used to invoke an arbitrary public method, which can<br />

return a result. A similar feature of the unified EL is functions. Method expressions differ from<br />

functions in many ways. “Functions” on page 164 explains more about the differences between<br />

functions and method expressions.<br />

Method expressions primarily benefit <strong>Java</strong>Server Faces technology, but they are available to any<br />

technology that can support the unified expression language. Let’s take a look at how <strong>Java</strong>Server<br />

Faces technology employs method expressions.<br />

In <strong>Java</strong>Server Faces technology, a component tag represents a UI component on a page. <strong>The</strong><br />

component tag uses method expressions to invoke methods that perform some processing for<br />

the component. <strong>The</strong>se methods are necessary for handling events that the components generate<br />

and validating component data, as shown in this example:<br />

<br />

<br />

<br />

<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!