Home > Articles > Programming > Java

The JSTL Expression Language

  • Print
  • + Share This
Examine the JSTL expression language in detail, starting with expressions and identifiers, and ending with sections on using the expression language for custom action attributes and common mistakes that developers make when using the expression language.
This chapter is from the book

Topics in This Chapter

  • Expression Language Overview

  • Expressions

  • Identifiers

  • Operators

  • Type Coercion

  • Literal Values

  • Implicit Objects

  • Method Invocation

  • EL Expressions in Custom Actions

  • Common Mistakes

2.1 Expression Language Overview

Although JSTL, as its name implies, provides a set of standard tags, its single most important feature may well be the expression language it defines. That expression language greatly reduces the need for specifying tag attribute values with Java code and significantly simplifies accessing all sorts of application data, including beans, arrays, lists, maps, cookies, request parameters and headers, context initialization parameters, and so on. In fact, the JSTL expression language adds so much value to JSP that it will be incorporated into JSP 2.0.1

This chapter examines the JSTL expression language in detail, starting with expressions and identifiers and ending with sections on using the expression language for custom action attributes and common mistakes that developers make when using the expression language.

NOTE

To illustrate the JSTL expression language, this chapter uses a number of JSTL actions, such as <c:out>, <c:if>, and <c:forEach>, that have not yet been formally discussed in this book. However, the use of those actions is intuitive and this chapter does not use any of those action's advanced features. See Chapter 3, "General-Purpose and Conditional Actions," and "Iteration Actions" on page 150 for formal discussions of the actions used throughout this chapter.

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:

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

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

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

  • + Share This
  • 🔖 Save To Your Account