Expression Language
表达式语言 EL(Expression Language) 是 JSP 页面中的一种脚本语言,语法简单,使用方便。基本格式是 ${expression}
。从 JSP2 开始,建议页面中尽量使用表达式语言,尽量避免在页面中使用 Java 脚本。以下四个部分均来自 Tomcat 自带的例子,位于 {tomcat install home}/webapps/examples/jsp/jsp2/el 目录下。
Basic Arithmetic 基本计算
EL 支持的基本计算有加、减、乘、除、求余、选择表达式。EL 式子的书写形式为 ${expression}
,客户浏览时候看到的就是对应计算结果了。Tomcat 的 {tomcat install home}/webapps/examples/jsp/jsp2/el/basic-arithmetic.jsp 上列举了一些例子,已能很好地说明问题,我在表的最後一列增添一些注释。如下
对于上表,tomcat 的描述是:This example illustrates basic Expression Language arithmetic. Addition (+), subtraction (-), multiplication (*), division (/ or div), and modulus (% or mod) are all supported. Error conditions, like division by zero, are handled gracefully.
Basic Comparisons 比较计算
比较计算的操作符是大于、小于、等于、大于或等于、小于或等于、不等于几种,运算符是数值或者字符或者字符串等。{tomcat install home}/webapps/examples/jsp/jsp2/el/basic-comparisons.jsp 上的原文是:The following comparison operators are supported:
* Less-than (< or lt) 小于
* Greater-than (> or gt) 大于
* Less-than-or-equal (<= or le) 小于或等于
* Greater-than-or-equal (>= or ge) 大于或等于
* Equal (== or eq) 等于
* Not Equal (!= or ne) 不等于
例子如下:
数值的 Numeric
字符或字符串的 Alphabitic
Implicit Objects 隐含对象
按照{tomcat install home}/webapps/examples/jsp/jsp2/el/implicit-objects.jsp 的说法,EL 支持的隐含对象有如下11种:
- pageContext - the PageContext object 页面内容对象
- pageScope - a Map that maps page-scoped attribute names to their values
- requestScope - a Map that maps request-scoped attribute names to their values
- sessionScope - a Map that maps session-scoped attribute names to their values
- applicationScope - a Map that maps application-scoped attribute names to their values
- param - a Map that maps parameter names to a single String parameter value
- paramValues - a Map that maps parameter names to a String[] of all values for that parameter
- header - a Map that maps header names to a single String header value
- headerValues - a Map that maps header names to a String[] of all values for that header
- initParam - a Map that maps context initialization parameter names to their String parameter value
- cookie - a Map that maps cookie names to a single Cookie object.
假设页面中有一个text对象,代码如下,
foo = <input type="text" name="foo" value="bar">
我在本机可以得到下表的内容:
从上面的表中可以看出隐含对象的使用方式,即 ${implicitObject.propertyName}
or ${implicitObject["propertyName"]}
.
这部分的内容,可详细参考Unified Expression Language (The Java EE 5 Tutorial, SUN).
EL 的隐含对象有11个,JSP 中的 servlet 9个,它们之间有很大的不同。对 JSP 中的 servlet,请参考这里。
(TODO 各隐含对象的默认属性有哪些)
隐含对象也有一定的优先级的,比如下面的例子:
<%
request.setAttribute("hello", "hello from request");
session.setAttribute("hello", "hello from session");
%>
${sessionScope.hello }<br/>
${requestScope.hello }<br/>
${hello }
输出是:
hello from session
hello from request
hello from request
从上面的例子看来,最好明确指出 scope 去调用,以免误解。
Functions 函数
按:本节内容涉及 JSP 标签的使用,更详细的标签使用介绍,参 JSP 标签的多种实现。
参考 {tomcat install home}/webapps/examples/jsp/jsp2/el/functions.jsp,自己走了一遍,整个 Eclipse 工程打包在附件里,部分文件的树图如下:
├─src
│ └─hello
│ Functions.java
│
└─WebContent
│ function.jsp
│
└─WEB-INF
│ web.xml
│
├─lib
└─tlds
functions.tld (经过试验,这个文件的後缀名可以改成 xml,当然对应的引用描述也要修改,Tomcat6.0.16。不过最好还是用这个 tld 的文件後缀名,以方便容器自动加载。)
使用的函数的整个流程如下:
1. 建立函数
具体就是创建一个普通的 Java 类,所谓函数,就是类里的方法,注意,一定要把属性设成 public static
才能被调用。我这里建的是 hello.Functions 这个类。
2. 添加描述符
把函数添加到标签描述文件 WebContent/tags/functions.tld 中,下面其中一个函数的描述信息,整个标签文件内容是这里。注意
<function>
<description>Get the length of the given String</description>
<name>getStringSize</name>
<function-class>hello.Functions</function-class>
<function-signature>int getStringSize( java.lang.String )</function-signature>
</function>
3. 引用描述符
在 web.xml 中添加 tld 文件的引用信息,加载<jsp-config>标签里(整个 web.xml 的内容在这里):
<jsp-config>
<taglib>
<taglib-uri> http://jakarta.apache.org/tomcat/functions
</taglib-uri>
<taglib-location> /WEB-INF/tags/functions.tld
</taglib-location>
</taglib>
</jsp-config>
4. 调用函数
- 在页面中通过 EL 调用函数。首先在页首声明刚才在 web.xml 中定义的标签
<%@ taglib prefix="fn" uri="http://jakarta.apache.org/tomcat/functions"%>
- 然後在正文按
${prefixName:functionName(args)}
的格式调用即可。带有批注的完整的 JSP 文件在这里。
启动 tomcat 後,可以看到如下的运行效果
To reverse "Hello, world!": "!dlrow ,olleH",
The length of "Hello, world!" is 13.