background image

Seting Tag Attribute Values

<< Where Value Expressions Can Be Used | Method Expressions >>
<< Where Value Expressions Can Be Used | Method Expressions >>

Seting Tag Attribute Values

There are three ways to set a tag attribute value using either an rvalue or lvalue expression:
With a single expression construct:
<some:tag value=
"${expr}"/>
<another:tag value=
"#{expr}"/>
These expressions are evaluated and the result is coerced to the attribute's expected type.
With one or more expressions separated or surrounded by text:
<some:tag value=
"some${expr}${expr}text${expr}"/>
<another:tag value=
"some#{expr}#{expr}text#{expr}"/>
These kinds of expression are called a composite expressions. They are evaluated from left to
right. Each expression embedded in the composite expression is coerced to a String and
then concatenated with any intervening text. The resulting String is then coerced to the
attribute's expected type.
With text only:
<some:tag value=
"sometext"/>
This expression is called a literal expression. In this case, the attribute's String value is
coerced to the attribute's expected type. Literal value expressions have special syntax rules.
See
"Literal Expressions" on page 158
for more information. When a tag attribute has an
enum type, the expression that the attribute uses must be a literal expression. For example,
the tag attribute can use the expression "hearts" to mean Suit.hearts. The literal is
coerced to Suit and the attribute gets the value Suit.hearts.
All expressions used to set attribute values are evaluated in the context of an expected type. If
the result of the expression evaluation does not match the expected type exactly, a type
conversion will be performed. For example, the expression ${1.2E4} provided as the value of
an attribute of type float will result in the following conversion:
Float.valueOf(
"1.2E4").floatValue()
See section 1.18 of the JavaServer Pages 2.1 Expression Language Specification (available from
http://jcp.org/aboutJava/communityprocess/final/jsr245/
) for the complete type
conversion rules.
Method Expressions
Another feature of the unified expression language is its support of deferred method
expressions. A method expression is used to invoke an arbitrary public method, which can
Unified Expression Language
The Java EE 5 Tutorial · September 2007
154