Home TOC |
![]() ![]() ![]() |
Expression Language Support
A primary feature of JSTL is its support for an expression language. Currently, a page author has to use an expression <%= aName %> to access the value of a system or user-defined JavaBeans component. For example:
<x:aTag att="<%= pageContext.getAttribute("aName") %>">Furthermore, referring to nested bean properties is even more complex:
<%= aName.getFoo().getBar() %>This makes page authoring more complex than it need be. An expression language allows a page author to access an object using a simplified syntax such as
<x:atag att="$aName"><x:aTag att="$aName.foo.bar">Expression languages elevate JSP scoped attributes as the standard way to communicate information from business logic to JSP pages. An expression language, in concert with JSTL tags, makes it possible to easily access application data and manipulate it in simple ways without having to use scriptlets or request-time expressions. For example, this conditional tag tests whether the number of items in a session-scoped shopping cart is greater than 0.
<c:if test="$session:cart.numberOfItems > 0"> ... </c:if>A goal of the next version of the JSP specification is to standardize on an expression language for all custom tag libraries. In the meantime, JSTL contains several expression languages for experimenting with, including:
- SPEL, the Simplest Possible Expression Language
- ECMAScript
- JXPath
The default expression language for the JSTL implementation is ECMAScript. However, the example discussed in this chapter uses SPEL (see Simplest Possible Expression Language (SPEL)) because it allows you to specify the scope of objects.
Twin Libraries
The JSTL tag libraries comes in two versions which differ only in the way they support the use of runtime expressions for attribute values.
In the JSTL-RT tag library, expressions are specified in the page's scripting language. This is exactly how things currently work in current tag libraries.
In the JSTL-EL tag library, expressions are specified in a JSTL expression language (EL). An expression is a
String
literal in the syntax of the EL. It is the responsibility of the EL to define the metacharacter(s) used to discriminate expressions fromString
literals (for example, use$
at the beginning of an expression).When using the EL tag library you cannot pass a scripting language expression for the value of an attribute. This rule makes it possible to validate the syntax of an expression at translation time.
Specifying an Expression Language Evaluator
To use an expression language other than the default (ECMAScript) you must provide a context parameter
javax.servlet.jsp.ExpressionEvaluatorClass
in the Web application deployment descriptor. Because the Duke's Bookstore example uses SPEL, which is not the default expression language, it must convey this information to the tag library implementation. Here is the declaration from the Duke's Bookstore descriptor:<context-param> <param-name> javax.servlet.jsp.jstl.temp.ExpressionEvaluatorClass </param-name> <param-value> org.apache.taglibs.standard.lang.spel.Evaluator </param-value> </context-param>It is also possible to override the default expression language setting with the
expressionLanguage
tag. For example:<c:expressionLanguage class="..."> <c:forEach items_="products/@key"> ... </c:forEach> </c:expressionLanguage>The scope of the expression language specified by tag
c:expressionLanguage
is limited to its body.c:expressionLanguage
tags can be nested, where nested occurrences shadow their ancestors.Simplest Possible Expression Language (SPEL)
This is a brief summary of SPEL. For a complete syntax, see <
JWSDP_HOME
>/docs/jstl/spel/spel.html
.SPEL is responsible for handling both expressions and literals. The syntax of SPEL is extremely simple. Expressions begin with a
$
character. For example:<c:if test="$bean1.a < 3" />Any value that does not begin with
$
is treated as a literal that is parsed to the expected type using thePropertyEditor
for the expected type:<c:if test="true" />Literal values that start with the $ character must be escaped using the
\
character:<mytags:price price="\$3.95" />If a value starts with
\$
, it is treated as a literal value with the leading\
removed.Attributes
Attributes are accessed by name, with an optional scope. Properties of attributes are accessed using the
.
operator, and may be nested arbitrarily. Indexed properties are accessed using the[]
operator.Attribute and property names must be Java identifiers, unless they are quoted.
If an attribute is specified with a scope of
page
,request
,session
, orapp
, its value is the value of that name in the given scope. If no scope is given, the value is found according to the rules ofPageContext.findAttribute(name)
.If an attribute is specified with a scope of
header
, its value is obtained by callingHttpServletRequest.getHeader(String)
.If an attribute is specified with a scope of
param
, its value is obtained by callingServletRequest.getParameter(String)
.If an attribute is specified with a scope of
paramvalues
, its value is obtained by callingServletRequest.getParameterValues(String)
.In any of these cases, if a value is not found, the result is an
ExpressionException
, notnull
.Relational Operators
Relational comparisons are allowed using the relational operators (
==
,!=
,<
,>
,<=
,>=
). Comparisons may be made against other values, or against boolean, string, integer, or floating point literals.Tag Collaboration
Tags usually collaborate with their environment in implicit and/or explicit ways. Implicit collaboration is done via a well defined interface that allows nested tags to work seamlessly with the ancestor tag exposing that interface. The JSTL iterator tags support this mode of collaboration.
Explicit collaboration happens when a tag exposes information to its environment. Traditionally, this has been done by exposing a scripting variable (with a JSP scoped attribute providing the actual object). Because JSTL supports an expression language, there is less need for scripting variables. So the JSTL tags (both the EL and RT versions) expose information only as JSP scoped attributes; no scripting variables are used. The convention JSTL follows is to use the name
var
for any tag attribute that exports information about the tag. For example, theforEach
tag exposes the current item of shopping cart it is iterating over in the following way:<c:forEach var="item" items="$session:cart.items"> ... </c:forEach>The name
var
was selected to highlight the fact that the scoped variable exposed is not a scripting variable (which is normally the case for attributes namedid
).In situations where a tag exposes more than one piece of information, the name
var
is used for the primary piece of information being exported, and an appropriate name is selected for any other secondary piece of information exposed. For example, iteration status information is exported by theforEach
tag via the attributestatus
.
Home TOC |
![]() ![]() ![]() |