EL 表达式
EL,Expression Language,表达式语言,是一种在 JSP 页面中获取数据的简单方式。EL表达式是从 JSP2.0 版本开始才引入的概念。EL 表达式的基本语法形式很简单:在 JSP 页面的任何静态部分均可通过${expression}的形式获取到指定表达式的值。
获取数据
(1) 从四大域中依次查找数据EL 只能从 pageConext、request、session、application 四大域属性空间中获取数据。以下方式是无法获取到指定数据的。因为这个数据没有存放在四大域属性空间中。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
int sum = 100;
%>
<%-- sum值并没有放到对应的四大域属性中,是无法取出 --%>
sun = ${sum}
此时,可以将这个值存放到四大域属性空间中的任意一个中。然后访问其存放时的 key即可。
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ page import="com.jsp.pojo.Student" %>
<%
Student student = new Student("小王", 88);
// request.setAttribute("stu", student);
// session.setAttribute("stu", student);
// application.setAttribute("number", number);
pageContext.setAttribute("student", student);
%>
sun = ${student}
我们发现,无论是将变量存放到了哪一个域属性空间,通过${key}的方式,均可访问到该数据的值。EL 到底是从哪个域空间中查找指定的 key 的呢?其底层实际是从最小范围依次查找,直到查找到最大范围 application。这期间,只要查找到了,则直接获取,后面的域空间将不再查找。若最终没有查找到,则什么也不输出。
注意: 页面没有开启El表达式,默认是禁用的
2) 从指定域中获取数据
从 pageContext 依次查找到 application 域空间,会降低执行效率。若某属性确定存放在某个域属性空间,则可指定直接从该空间中查找。此时需要借助 EL 的四个域属性空间相关的内置对象。
<%
Student student = new Student("小王", 88);
pageContext.setAttribute("stu", student);
request.setAttribute("stu", student);
session.setAttribute("stu", student);
application.setAttribute("stu", student);
%>
sun = ${pageScope.stu} <br>
sun = ${requestScope.stu} <br>
sun = ${sessionScope.stu} <br>
sun = ${applicationScope.stu} <br>
(3) 访问 Bean 的属性
EL 可以通过 ” k e y . 属性 “的方式获取到指定对象的指定属性值。其底层实际调用的是该对象的相应属性的 g e t 方法。当然,也可以使用 {key.属性} “的方式获取到指定对象的指定属性值。其底层实际调用的是该对象的相应属性的 get 方法。当然,也可以使用 key.属性“的方式获取到指定对象的指定属性值。其底层实际调用的是该对象的相应属性的get方法。当然,也可以使用{key[‘属性’]或${key[“属性”]的方式获取。该方式不常用。
<%
Student student = new Student("小王", 88);
pageContext.setAttribute("stu", student);
request.setAttribute("stu", student);
session.setAttribute("stu", student);
application.setAttribute("stu", student);
%>
sun = ${pageScope.stu.name} <br>
sun = ${requestScope.stu.age} <br>
sun = ${sessionScope.stu["name"]} <br>
sun = ${applicationScope.stu['name']} <br>
当然,若要访问一个对象的域属性的值,则可多次使用点号运算符,依次取出相应的属性值。
(4) 获取数组中的元素
EL 可以通过 ${key[索引]} 的方式获取到指定索引的元素。不过,需要注意的是,若数组中不存在该指定索引的元素,系统并不会抛出数组越界异常。
<%
String[] names = {"小小","牛牛","花花"};
pageContext.setAttribute("names",names);
%>
<%--输出 牛牛 下标索引都是从 0 开始--%>
${pageScope.names} <br>
${pageScope.names[1]} <br>
<%-- 不会出现数组越界--%>
${pageScope.names[100]}
(5) 获取 List 中的元素与获取数组中的元素相同,通过 ${key[索引]} 的方式可以获取 List 中指定索引的元素。若 List 中不存在该指定索引的元素,系统并不会抛出越界异常。
<%
Student xiao = new Student("xiao", 90);
Student xia = new Student("xia", 34);
Student x = new Student("x", 45);
ArrayList<Student> students = new ArrayList<>();
students.add(xiao);
students.add(xia);
students.add(x);
session.setAttribute("studebt", students);
%>
<%--输出 下标索引都是从 0 开始--%>
${sessionScope.studebt} <br>
${sessionScope.studebt[1]} <br>
<%-- 不会出现数组越界--%>
${sessionScope.studebt[100]}
不过,需要注意的是,EL 无法输出 Set 集合中的元素。因为 Set 集合中的元素具有无序性,即没有索引的概念。无法通过索引获取元素。
(6) 获取 Map 中的元素EL 通过 ${attributeName.mapKey} 的方式可以获取指定 Map 的指定 key 的值。
<%
HashMap<String, Student> studentMap = new HashMap<>();
Student xioami = new Student("小米", 78);
studentMap.put("xiaomi", xioami);
application.setAttribute("studentMap",studentMap);
%>
${applicationScope.studentMap.xiaomi} <br>
运算符
EL 表达式可以进行各种运算,其中常用的运算符有:
除了上述运算符外,还有一个非常有用的运算符 empty,其用法为${empty 变量},结果为布尔值。
若变量未定义,则返回值为 true
若变量为 String 类型,且其值为空串,则返回值为 true。
若变量为引用类型,且其值为 null,则返回值为 true。
若变量为集合类型,且其不包含任何元素,则返回值为 true。
<%
pageContext.setAttribute("name", "");
request.setAttribute("name", null);
session.setAttribute("map", new HashMap<>());
application.setAttribute("list", new ArrayList<>());
%>
${empty pageScope.name}
${empty requestScope.name}
${empty sessionScope.map}
${empty applicationScope.list}
EL 内置对象
就像 JSP 的 Java 代码块及表达式块中可以使用九个内置对象一样,EL 表达式中,同样也存在有内置对象,并且存在 11 个内置对象。常用的内置对象,除了前面使用过的四个域属性空间相关的内置对象外,还有如下几个。
(1) pageContext
该 pageContext 与 JSP 内置对象中的 pageContext 是同一个对象。通过该对象,可以获取到 request、response、session、servletContext、servletConfig 等对象。注意,这些对象在 EL中不是内置对象。这些对象只能通过 pageContext 获取。
在 EL 中直接$
{pageContext.request}即可获取 request 对象。当然,其底层实际调用的是$
pageContext.getRequest()方法。同理,也可以通过类似方式获取到其它对象。
在这些获取的对象中,有一个是实际工程中最常用的:{pageContext.request.contextPath} ,用于获取当前项目的发布到服务器的名称。一般会用在 JSP 页面的路径前。
<form action="${pageContext.request.contextPath}/list.do" method="get">
</form>
发布到服务器的名称: ${pageContext.request.contextPath}
在 EL 的 11 个内置对象中,除了 pageContext 外,其它 10 个内置对象,其类型均为java.util.Map 类型。
(2) param
在 EL 中通过 ${param.参数名}可获取到请求中指定参数名的值。例如,提交的请求为:
http://localhost:8080/java_web_jsp/index.jsp?age=345
在 JSP 页面中通过如下方式,可获取到 name 参数的值为 abc。
${param.age}
(3) paramValues
若提交的请求中同一参数具有多个值,则可通过 ${paramValues.参数名[索引]} 获取到指定索引号的该参数值。例如,提交的请求为
http://localhost:8080/java_web_jsp/index.jsp?name=%E5%B0%8F%E8%8A%B1&name=%E5%B0%8F%E6%9C%A8
:在 JSP 页面中获取方式如下:
参数1: ${paramValues.name[0]}
参数2: ${paramValues.name[1]}
路径为:
http://localhost:8080/java_web_jsp/index.jsp?name=小草&npm=小脑幕&age=89
JSP的提取方法:
参数1: ${param.name}
参数2: ${param.npm}
参数3: ${param.age}
(4) initParam
在 EL 中通过使用 ${initParam.初始化参数名} 可以获取到指定的初始化参数的值。例如,在 web.xml 中定义了初始化参数 xxxName。
<context-param>
<param-name>XXXName</param-name>
<param-value>XVALUE</param-value>
</context-param>
在 JSP 的 EL 中可访问该初始化参数:
初始化参数值为: ${initParam.XXXName}
自定义 EL 函数
EL 中存在一个问题:不支持字符串的操作。就连最简单的字符串连接,例如${“ab” + “cd”}都不支持,运行会报错。更不要提将字符串中的字符转换为全大写、判断一个字符串中是否包含另一个子串等操作,EL 本身更是无法完成。但 EL 支持自定义函数,可以在 EL 中调用自定义的函数来完成这些功能。下面以实现“将一个字符串转换为全大写”功能为例,来讲解自定义 EL 函数的过程。
(1) 定义函数
定义一个 POJO 类,在该类中定义一个静态方法。该方法可以实现将字符串转换为全大写字母的功能。
package com.jsp.unti;
public class StringUtils {
public static String lowerToUpper(String source) {
return source.toUpperCase();
}
}
(2) 注册函数
在 Web 项目的/WEB-INF 目录下,新建一个扩展名为.tld 的 XML 文件,例如 myfn.tld。tld,即 Tag Library Definition,标签库定义。将定义好的函数,在这个文件中进行注册。
A、添加约束XML
文件是需要在文件中添加相应约束的,即添加配置文件头部信息。这个头部信息可以从 Tomcat 的 webapps 中的/examples/WEB-INF/jsp2/jsp2-example-taglib.tld 文件中复制。
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
B、 添加其它子标签
在根标签下添加其它子标签。
<tlib-version>1.0</tlib-version>
<short-name>myfn</short-name>
<uri>http://www.xxx.com/jsp/myCustomTld</uri>
1.0
指定定义的当前函数库版本号。随意指定。
myfn
指定该函数库的名称,一个函数库一个名称,即一个 tld 文件一个。名称随意,但一般与文件名相同,这个名称在后面 JSP 中要使用。
http://www.xxx.com/jsp/myCustomTld
指定该函数库所对应的 URL,一个函数库一个 URL,即一个 tld 文件一个 URL。URL 随意,在后面 JSP 中要使用该 URL。
C、 注册函数在
根标签的子标签下添加子标签,注册函数。
<function>
<name>lowerToUpper</name>
<function-class>com.jsp.unti.StringUtils</function-class>
<function-signature>java.lang.String lowerToUpper(java.lang.String)</function-signature>
</function>
myLowerToUpper指定将来在 JSP 的 EL 中使用该函数的名称。一般与类中静态方法同名。
com.abc.functions.StringFunctions
指定该函数定义在哪个类中。
java.lang.String lowerToUpper( java.lang.String )
指定该函数的签名,即在指定类中的哪个方法。
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>myfn</short-name>
<uri>http://www.xxx.com/jsp/myCustomTld</uri>
<function>
<name>lowerToUpper</name>
<function-class>com.jsp.unti.StringUtils</function-class>
<function-signature>java.lang.String lowerToUpper(java.lang.String)</function-signature>
</function>
</taglib>
(3) 使用函数在
JSP 页面中若要使用自定义的 EL 函数,则需要如下几步:
A、引入自定义的函数库使用 taglib 指令将自定义的函数库引入。
<%-- 引入自定义标签库 --%>
<%@ taglib uri="http://www.xxx.com/jsp/myCustomTld" prefix="myfn"%>
B、 在 EL 中使用函数需要注意的是,EL 是不能处理域属性空间以外的变量的。所以,EL 函数也是无法处理域属性空间以外的变量的。
<%--函数中可以直接将字符串常量直接作为参数--%>
字符变量: str = ${myfn:lowerToUpper("jkhgh")}<br>
<%--函数中可以将域属性值作为参数--%>
<%
String stre = "Akjhd";
pageContext.setAttribute("sum", stre);
%>
字符变量: str = ${myfn:lowerToUpper(pageScope.sum)}<br>
<%--函数无法操作不在域属性空间中的变量--%>
<%
String stree = "Akjhd";
%>
字符变量: str = ${myfn:lowerToUpper(stree)}<br>
JSTL 中的 EL 函数
(1) JSTL
Apache 已经定义好了一套标准的标签库规范,称为 JSTL,JSP Standard Tag Library,即 JSP标准标签库。该规范已通过 JCP 审核认定。
在 JSTL 中,已经定义好了一套对于字符串进行处理的函数标签库,这个函数标签库中定义了 16 个对于字符串进行处理的函数。
我们在 JSP 页面中可以直接使用。当然,需要使用 JSTL,首先需要将其 Jar 包导入。
(2) JSTL 的 EL 函数标签库JSTL函数标签库的.tld文件存放于standard的jar包的META-INF目录中,文件名为fn.tld。
打开 fn.tld 文件,可以看到其为 fn,即将来 JSP 中使得的 prefix 前辍为 fn,uri 为 http://java.sun.com/jsp/jstl/functions。然后,其中定义了 16 个对于字符串进行处理的函数。
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>JSTL 1.1 functions library</description>
<display-name>JSTL functions</display-name>
<tlib-version>1.1</tlib-version>
<short-name>fn</short-name>
<uri>http://java.sun.com/jsp/jstl/functions</uri>
<function>
<description>
Tests if an input string contains the specified substring.
</description>
<name>contains</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>boolean contains(java.lang.String, java.lang.String)</function-signature>
<example>
<c:if test="${fn:contains(name, searchString)}">
</example>
</function>
<function>
<description>
Tests if an input string contains the specified substring in a case insensitive way.
</description>
<name>containsIgnoreCase</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>boolean containsIgnoreCase(java.lang.String, java.lang.String)</function-signature>
<example>
<c:if test="${fn:containsIgnoreCase(name, searchString)}">
</example>
</function>
<function>
<description>
Tests if an input string ends with the specified suffix.
</description>
<name>endsWith</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>boolean endsWith(java.lang.String, java.lang.String)</function-signature>
<example>
<c:if test="${fn:endsWith(filename, ".txt")}">
</example>
</function>
<function>
<description>
Escapes characters that could be interpreted as XML markup.
</description>
<name>escapeXml</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>java.lang.String escapeXml(java.lang.String)</function-signature>
<example>
${fn:escapeXml(param:info)}
</example>
</function>
<function>
<description>
Returns the index withing a string of the first occurrence of a specified substring.
</description>
<name>indexOf</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>int indexOf(java.lang.String, java.lang.String)</function-signature>
<example>
${fn:indexOf(name, "-")}
</example>
</function>
<function>
<description>
Joins all elements of an array into a string.
</description>
<name>join</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>java.lang.String join(java.lang.String[], java.lang.String)</function-signature>
<example>
${fn:join(array, ";")}
</example>
</function>
<function>
<description>
Returns the number of items in a collection, or the number of characters in a string.
</description>
<name>length</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>int length(java.lang.Object)</function-signature>
<example>
You have ${fn:length(shoppingCart.products)} in your shopping cart.
</example>
</function>
<function>
<description>
Returns a string resulting from replacing in an input string all occurrences
of a "before" string into an "after" substring.
</description>
<name>replace</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>java.lang.String replace(java.lang.String, java.lang.String, java.lang.String)</function-signature>
<example>
${fn:replace(text, "-", "•")}
</example>
</function>
<function>
<description>
Splits a string into an array of substrings.
</description>
<name>split</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>java.lang.String[] split(java.lang.String, java.lang.String)</function-signature>
<example>
${fn:split(customerNames, ";")}
</example>
</function>
<function>
<description>
Tests if an input string starts with the specified prefix.
</description>
<name>startsWith</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>boolean startsWith(java.lang.String, java.lang.String)</function-signature>
<example>
<c:if test="${fn:startsWith(product.id, "100-")}">
</example>
</function>
<function>
<description>
Returns a subset of a string.
</description>
<name>substring</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>java.lang.String substring(java.lang.String, int, int)</function-signature>
<example>
P.O. Box: ${fn:substring(zip, 6, -1)}
</example>
</function>
<function>
<description>
Returns a subset of a string following a specific substring.
</description>
<name>substringAfter</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>java.lang.String substringAfter(java.lang.String, java.lang.String)</function-signature>
<example>
P.O. Box: ${fn:substringAfter(zip, "-")}
</example>
</function>
<function>
<description>
Returns a subset of a string before a specific substring.
</description>
<name>substringBefore</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>java.lang.String substringBefore(java.lang.String, java.lang.String)</function-signature>
<example>
Zip (without P.O. Box): ${fn:substringBefore(zip, "-")}
</example>
</function>
<function>
<description>
Converts all of the characters of a string to lower case.
</description>
<name>toLowerCase</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>java.lang.String toLowerCase(java.lang.String)</function-signature>
<example>
Product name: ${fn.toLowerCase(product.name)}
</example>
</function>
<function>
<description>
Converts all of the characters of a string to upper case.
</description>
<name>toUpperCase</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>java.lang.String toUpperCase(java.lang.String)</function-signature>
<example>
Product name: ${fn.UpperCase(product.name)}
</example>
</function>
<function>
<description>
Removes white spaces from both ends of a string.
</description>
<name>trim</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>java.lang.String trim(java.lang.String)</function-signature>
<example>
Name: ${fn.trim(name)}
</example>
</function>
</taglib>
(3) 16 个 EL 函数
JSTL 的函数标签库中定义的这 16 个 EL 函数如下表所示:
(4) EL 函数用法
在 JSP 页面中使用 JSTL 的 EL 函数,首先需要使用 taglib 指令将 JSTL 的函数标签库引入。需要注意的是,EL 函数是无法操作非域属性中的变量的。
首先引入JSTL函数标签库
<%-- 引入JSTL函数标签库 --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%--函数中可以直接将字符串常量直接作为参数--%>
字符变量: str = ${fn:toUpperCase("jkhgh")}<br>
<%--函数中可以将域属性值作为参数--%>
<%
String stre = "Akjhd";
pageContext.setAttribute("sum", stre);
%>
字符变量: str = ${fn:substring(pageScope.sum,0,3)}<br>
<%--函数无法操作不在域属性空间中的变量--%>
<%
String stree = "Akjhd";
%>
字符变量: str = ${fn:toUpperCase(stree)}<br>
EL 总结
EL 不能出现在 Java 代码块、表达式块等 JSP 的动态代码部分。
EL 只能从 pageConext、request、session、application 四大域属性空间中获取数据。
EL 不会抛出空指针异常。若访问一个 null 对象的属性,则什么也不显示。
EL 不会抛出数组访问越界异常。若访问一个数组中的不存在的元素,则什么也不显示。
EL 不具有对字符串进行处理的能力,就连简单的字符串拼接都不行。