JSP Expression Language
Celsina Bignoli
bignolic@smccd.net
Expression Language
•
Introduced as part of standard JSP in version 2.0
•
simple expression language for accessing
variables
•
starts with the
${
delimiter and ends with
}
${anExpression}
•
Case sensitive. All keywords are lowercase
Using Expressions
•
in static text
–
The value of the expression is computed and inserted
into the current output
•
In any standard or custom tag attribute that can
accept an expression
<some:tag value="${expr}"/>
–
The expression is evaluated and the result is coerced to
the attribute's expected type
<some:tag
value="some${expr}${expr}text${expr}"/>
–
The expressions are evaluated from left to right. Each
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.
Variables
•
The web container evaluates a variable that
appears in an expression by looking up its value
according to the behavior of
PageContext.findAttribute(String).
•
For example, when evaluating the expression
${product}, the container will look for product in the
following scopes:
–
page
–
request
–
session
–
application
•
If product is not found, null is returned.
Variables
•
Properties of variables are accessed using the
“. “
operator and can be nested arbitrarily
•
For example:
–
${pageContext.session}
–
${myBean.attribute1.attribute2}
Implicit Variables
•
pageContext
: The context for the JSP page.
Provides access to various other objects
including:
–
session
: The session object for the client.
–
request
: The request triggering the execution of the JSP
page.
–
response
: The response returned by the JSP page.
Implicit Variables
•
param
: Maps a request parameter name to a
single value
–
<c:out value=“${param.userName}” />
•
paramValues
: Maps a request parameter name to
an array of values
•
header
: Maps a request header name to a single
value
•
headerValues
: Maps a request header name to an
array of values
•
cookie
: Maps a cookie name to a single cookie
•
initParam
: Maps a context initialization parameter
name to a single value
Implicit Variables
•
pageScope
: Maps page
-
scoped variable names to
their values
•
requestScope
: Maps request
-
scoped variable
names to their values
•
sessionScope
: Maps session
-
scoped variable
names to their values
•
applicationScope
: Maps application
-
scoped
variable names to their values
Literals
•
The JSP expression language defines the
following literals:
–
Boolean
: true and false
–
Integer
: 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
Literals
•
the JSP expression language provides the
following operators:
–
Arithmetic
: +,
-
(binary), *, / and div, % and mod,
-
(unary)
–
Logical
: and, &&, or, ||, not, !
–
Relational
: ==, eq, !=, ne, <, lt, >, gt, <=, ge, >=, le.
Comparisons can be made against other values, or
against boolean, string, integer, or floating point literals.
–
Empty
: The empty operator is a prefix operation that can
be used to determine whether a value is null or empty.
–
Conditional
: A ? B : C. Evaluate B or C, depending on
the result of the evaluation of A.
Reserved Words
•
The following words are reserved for the
JSP expression language and should not be
used as identifiers:
and
eq
gt
true
instanceof
or
ne
le
false
empty
not
lt
ge
null
div
mod
Example
•
${4.0 > 3}
true
•
${empty param.add}
true if parameter add in the
request is null or empty string
•
${pageContext.request.contextPath}
The context
path
•
${sessionScope.cart.numberOfItems}
The value of
the numberOfItems property of the session
-
scoped attribute named cart
Enter the password to open this PDF file:
File name:
-
File size:
-
Title:
-
Author:
-
Subject:
-
Keywords:
-
Creation Date:
-
Modification Date:
-
Creator:
-
PDF Producer:
-
PDF Version:
-
Page Count:
-
Preparing document for printing…
0%
Σχόλια 0
Συνδεθείτε για να κοινοποιήσετε σχόλιο