2.1 Expression Language Overview

   

The JSTL expression language is a simple language inspired by ECMAScript (also known as JavaScript) and XPath. The expression language provides:

  • Expressions and identifiers

  • Arithmetic, logical, and relational operators

  • Automatic type coercion

  • Access to beans, arrays, lists, and maps

  • Access to a set of implicit objects and servlet properties

All of the features listed above are described in this chapter.

Throughout this book, for convenience the expression language is referred to with the acronym EL and JSTL expressions are referred to as EL expressions.

How the Expression Language Works

Nearly all of the JSTL actions have one or more dynamic attributes that you can specify with an EL expression; [2] for example, you can specify a request parameter with the <c:out> action's value attribute like this:

[2] Dynamic values for JSTL actions from the runtime (RT) library can be specified as JSP expressions.

 <c:out value='${param.emailAddress}'/> 

The preceding expression displays the value of a request parameter named emailAddress . You can also use EL expressions to perform conditional tests, for example:

 <c:if test='${not empty param.emailAddress}'>...</c:if> 

The body of the preceding <c:if> action is evaluated if the emailAddress request parameter is not empty, meaning neither null nor an empty string.

If you're using JSTL with JSP 1.2, you can only use JSTL expressions to specify values for JSTL action attributes, as illustrated above. [3] All JSTL actions that have dynamic attributes interpret EL expressions before they are passed to the action's tag handler, so the expression language is applied ”and values are typically coerced ”before the tag handler gets them.

[3] Starting with JSP 2.0, you will be able to use EL expressions in JSP template text. See "Expressions" on page 41 for more information.

How to Use the Expression Language

Attributes of JSTL actions can be specified with EL expressions in one of three ways. First, an attribute can be specified with a single expression like this: [4]

[4] The action <jstl:action> represents any JSTL action.

 <jstl:action value='${expr}'/> 

In the preceding code fragment, the expression ${expr} is evaluated and its value is coerced to the type expected by the value attribute.

Attribute values can also be specified as strings, like this:

 <jstl:action value='text'/> 

The string specified for the value attribute in the preceding code fragment is coerced to the type expected by that attribute.

Finally, attribute values can consist of one or more expressions intermixed with strings, like this:

 <jstl:action value='${expr}text${expr}${expr}more text${expr}'/> 

In the previous code fragment, each of the four expressions is evaluated in order from left to right, coerced to a string, and concatenated with the intermixed text. The resulting string is subsequently coerced to the value expected by the value attribute.

   


Core JSTL[c] Mastering the JSP Standard Tag Library
Core JSTL[c] Mastering the JSP Standard Tag Library
ISBN: 131001531
EAN: N/A
Year: 2005
Pages: 124

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net