Home TOC |
![]() ![]() ![]() |
Expression Language Support
A primary feature of JSTL is its support for an expression language (EL). 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") %>">Referring to nested bean properties is even more complex:
<%= aName.getFoo().getBar() %>This makes page authoring more complicated 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}">An expression language elevates 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>The next version of the JSP specification will standardize on an expression language for all custom tag libraries. This release of JSTL includes a snapshot of that expression language.
Twin Libraries
The JSTL tag libraries come 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 the JSTL expression language. An expression is a
String
literal in the syntax of the EL.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.
JSTL Expression Language
The JSTL expression language is responsible for handling both expressions and literals. Expressions are enclosed by the
${ }
characters. 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 contain the
${
characters must be escaped as follows:<mytags:example attr1="an expression is ${'${'}true}" />Attributes
Attributes are accessed by name, with an optional scope. Properties of attributes are accessed using the
.
operator, and may be nested arbitrarily.The EL unifies the treatment of the
.
and[]
operators. Thus,expr-a.expr-b
is equivalent toexpr-a[expr-b]
. To evaluateexpr-a[expr-b]
, evaluateexpr-a
intovalue-a
and evaluateexpr-b
intovalue-b
.
- If
value-a
is aMap
returnvalue-a.get(value-b)
.- If
value-a
is aList
or array coercevalue-b
toint
and returnvalue-a.get(value-b)
orArray.get(value-a, value-b)
, as appropriate.- If
value-a
is a JavaBeans object, coercevalue-b
toString
. Ifvalue-b
is a readable property ofvalue-a
the return result of getter call. Accessing indexed JavaBeans component properties will be added in a later release.The EL evaluates an identifier by looking up its value as an attribute, according to the behavior of
PageContext.findAttribute(String)
. For example,${product}
will look for the attribute namedproduct
, searching the page, request, session, and application scopes, and will return its value. If the attribute is not found, null is returned. Note that an identifier that matches one of the implicit objects described next will return that implicit object instead of an attribute value.The EL defines a set of implicit objects:
pageContext
- the PageContext objectpage
- aMap
that maps page-scoped attribute names to their valuesrequest
- aMap
that maps request-scoped attribute names to their valuessession
- aMap
that maps session-scoped attribute names to their valuesapplication
- aMap
that maps application-scoped attribute names to their valuesparam
- aMap
that maps parameter names to a single String parameter value (obtained by callingServletRequest.getParameter(String)
)params
- aMap
that maps parameter names to a String[] of all values for that parameter (obtained by callingServletRequest.getParameterValues(String))
When an expression references one of these objects by name, the appropriate object is returned instead of the corresponding attribute. For example:
${pageContext}
returns thePageContext
object, even if there is an existingpageContext
attribute containing some other value.Literals
- Boolean:
true
andfalse
- Long: as in Java
- Floating point: as in Java
- String: with single and double quotes.
"
is escaped as\"
,'
is escaped as\'
, and\
is escaped as\\
.- Null:
null
Operators
The EL provides the following operators:
- Arithmetic:
+
,-
,*
,/
anddiv
,%
andmod
,-
- Logical: are
and
,or
, andnot
- Relational:
==
,!=
,<
,>
,<=
,>=
. 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 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 has 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 the 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 |
![]() ![]() ![]() |