Expression Language
Expression Language
A primary feature of JSP technology version 2.0 is its support for an expression language (EL). An expression language makes it possible to easily access application data stored in JavaBeans components. For example, the JSP expression language allows a page
author to access a bean using simple syntax such as${name}
for a simple variable or${name.foo.bar}
for a nested
property.
Thetest
attribute of the following conditional tag is supplied with an EL expression that compares the number of items in the session-scoped bean namedcart
with
0:
The JSP expression evaluator is responsible for handling EL expressions, which are enclosed by the${ }
characters and can include literals. Here's an example:
Any value that does not begin with${
is treated as a literal and is parsed to the expected type using thePropertyEditor
for
the type:
Literal values that contain the${
characters must be escaped as follows:
Deactivating Expression Evaluation
Because the pattern that identifies EL expressions--${ }
--was not reserved in the JSP specifications before JSP 2.0, there may be applications where such a pattern is intended to pass
through verbatim. To prevent the pattern from being evaluated, you can deactivate EL evaluation.
To deactivate the evaluation of EL expressions, you specify theisELIgnored
attribute of thepage
directive:
The valid values of this attribute aretrue
andfalse
. If it istrue
,
EL expressions are ignored when they appear in static text or tag attributes. If it isfalse
, EL expressions are evaluated by the container.
The default value varies depending on the version of the web application deployment descriptor. The default mode for JSP pages delivered using a Servlet 2.3 or earlier descriptor is to ignore EL expressions; this provides backward compatibility. The default
mode for JSP pages delivered with a Servlet 2.4 descriptor is to evaluate EL expressions; this automatically provides the default that most applications want. You can also deactivate EL expression evaluation for a group of JSP pages (seeDeactivating
EL Expression Evaluation)
.
Using Expressions
EL expressions can be used:
The value of an expression in static text is computed and inserted into the current output. If the static text appears in a tag body, note that an expressionwill notbe evaluated if the body is declared
to betagdependent
(seebody-content Attribute).
There are three ways to set a tag attribute value:
- With a single expression construct:
- With one or more expressions separated or surrounded by text:
- With text only:
<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 aString
and then concatenated with any intervening text. The resultingString
is
then coerced to the attribute's expected type.
<some:tag value="sometext"/>
In this case, the attribute'sString
value is coerced to the attribute's expected type.
Expressions used to set attribute values are evaluated in the context of an expected type. If the result of the expression evaluation does not match the expected type exactly, a type conversion will be performed. For example, the expression${1.2E4}
provided
as the value of an attribute of typefloat
will result in the following conversion:
See section JSP2.8 of theJSP 2.0 specificationfor the complete type conversion rules.
Variables
The web container evaluates a variable that appears in an expression by looking up its value according to the behavior ofPageContext.findAttribute(String)
. For example, when evaluating
the expression${product}
, the container will look forproduct
in the page, request, session, and application
scopes and will return its value. Ifproduct
is not found,null
is returned. A variable that matches one of the
implicit objects described inImplicit Objectswill return that implicit object instead of the variable's value.
Properties of variables are accessed using the.
operator and can be nested arbitrarily.
The JSP expression language unifies the treatment of the.
and[]
operators.expr-a.identifier-b
is
equivalent to expr-a["identifier-b"]
; that is, the expressionexpr-b
is used to construct a literal whose value
is the identifier, and then the[]
operator is used with that value.
To evaluateexpr-a[expr-b]
, evaluateexpr-a
intovalue-a
and
evaluateexpr-b
intovalue-b
. If eithervalue-a
orvalue-b
is
null, returnnull
.
- If
value-a
is aMap
, returnvalue-a.get(value-b)
. If!value-a.containsKey(value-b)
, then returnnull
. - 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 the coercion couldn't be performed, an error is returned. If theget
call returns anIndexOutOfBoundsException
,null
is returned. If theget
call returns another exception, an error is returned. - If
value-a
is a JavaBeans object, coercevalue-b
toString
. Ifvalue-b
is a readable property ofvalue-a
, then return the result of aget
call. If theget
method throws an exception, an error is returned.
Implicit Objects
The JSP expression language defines a set of implicit objects:
-
pageContext
: The context for the JSP page. Provides access to various objects including: -
servletContext
: The context for the JSP page's servlet and any web components contained in the same application. SeeAccessing the Web Context. -
session
: The session object for the client. SeeMaintaining Client State. -
request
: The request triggering the execution of the JSP page. SeeGetting Information from Requests. -
response
: The response returned by the JSP page. SeeConstructing Responses.
In addition, several implicit objects are available that allow easy access to the following objects:
-
param
: Maps a request parameter name to a single value -
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
Finally, there are objects that allow access to the various scoped variables described inUsing Scope Objects.
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
The JSP expression language defines the following literals:
Operators
In addition to the.
and[]
operators discussed inVariables,
the JSP expression language provides the following operators:
- Arithmetic:
+
,-
(binary),*
,/
anddiv
,%
andmod
,-
(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 isnull
or empty. - Conditional:
A ? B : C
. EvaluateB
orC
, depending on the result of the evaluation ofA
.
The precedence of operators highest to lowest, left to right is as follows:
Reserved Words
The following words are reserved for the JSP expression language and should not be used as identifiers.
Note that many of these words are not in the language now, but they may be in the future, so you should avoid using them.
Examples
Table 12-2contains example EL expressions and the result of evaluating them.
Functions
The JSP expression language allows you to define a function that can be invoked in an expression. Functions are defined using the same mechanisms as custom tags (SeeUsing Custom Tagsand Chapter15).
Using Functions
Functions can appear in static text and tag attribute values.
To use a function in a JSP page, you use ataglib
directive to import the tag library containing the function. Then you preface the function invocation with the prefix declared in the
directive.
For example, the date example pageindex.jsp
imports the/functions
library and invokes the functionequals
in
an expression:
<%@ taglib prefix="f" uri="/functions"%> ... <c:when test="${f:equals(selectedLocaleString, localeString)}" >
Defining Functions
To define a function you program it as a public static method in a public class. Themypkg.MyLocales
class in thedate
example
defines a function that tests the equality of twoString
s as follows:
package mypkg; public class MyLocales { ... public static boolean equals( String l1, String l2 ) { return l1.equals(l2); } }
Then you map the function name as used in the EL expression to the defining class and function signature in a TLD. The followingfunctions.tld
file in the date example maps theequals
function
to the class containing the implementation of the functionequals
and the signature of the function:
<function> <name>equals</name> <function-class>mypkg.MyLocales</function-class> <function-signature>boolean equals( java.lang.String, java.lang.String )</function-signature> </function>
A tag library can have only onefunction
element that has any givenname
element.
上一篇: 小强的HTML5移动开发之路(9)——坦克大战游戏3
下一篇: 统计Oracle读块的IO能力
推荐阅读
-
C#简单实现表达式目录树(Expression)
-
关于SQL中CTE(公用表表达式)(Common Table Expression)的总结
-
css expression 隔行换色
-
C# regular expression to validate email
-
CSS expression控制图片自动缩放效果代码[兼容 IE,Firefox]
-
不要使用CSS Expression的原因分析
-
python中如何正确使用正则表达式的详细模式(Verbose mode expression)
-
AngularJs expression详解及简单示例
-
AS报错:lambda expressions are not supported at this language level
-
Microsoft Expression Web 简体中文正式版 官方下载地址