EL(表达式语言)
一、EL概述
EL(Expression Language)是一门表达式语言,它对应<%=…%>。我们知道在JSP中,表达式会被输出,所以EL表达式也会被输出。
1.EL的格式
${…}
2.EL运算符
![](https://img.haomeiwen.com/i7900193/9f2557425940e9b8.png)
3.EL不显示null
当EL表达式的值为null时,会在页面上显示空白,即什么都不显示。
二、EL表达式读取四大域
-
${xxx}
全域查找名为xxx的属性
如果不存在,输出空字符串,而不是null -
${pageScope.name}
获取pageContext域属性 -
${requestScope.name}
获取request域属性 -
${sessionScope.name}
获取session域属性 -
${applicationScope.name}
获取ServletContext域属性
例如:
EL来访问JavaBean属性
Person.java
public class Person {
private String name;
private int age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
![](https://img.haomeiwen.com/i7900193/d14e9bdb0cfe03c2.jpg)
三、EL内置对象
EL一共11个内置对象,无需创建即可以使用。
pageScope,requestScope,sessionScope,applicationScope,param,paramValues,header,headerValues,initParam,cookie,pageContext
除了pageContext其余的都是Map类型
EL使用语法
${Map.key}
或
${map['key']}
- 请求参数相关内置对象
- param
Map<String,String>类型
param对象可以用来获取参数,与request.getParameter()方法相同。 - paramValues
paramValues是Map<String, String[]>类型,当一个参数名,对应多个参数值时可以使用它。
- param
![](https://img.haomeiwen.com/i7900193/8961d6472ae7ca81.jpg)
![](https://img.haomeiwen.com/i7900193/cfa6050485193c8a.jpg)
-
请求头相关内置对象
-
header
Map<String,String>类型
用来获取请求头
例如:${header['User-Agent']}
-
headerValues
headerValues是Map<String,String[]>类型。
当一个请求头名称,对应多个值时,使用该对象
-
-
应用初始化参数相关内置对象
- initParam
initParam是Map<String,String>类型
它对应web.xml文件中的<context-param>参数。
- initParam
![](https://img.haomeiwen.com/i7900193/d19b0e5161e95bdf.jpg)
- Cookie相关内置对象
- cookie是Map<String,Cookie>类型,其中key是Cookie的名字,而值是Cookie对象本身。
例如:${cookie.JSESSIONID.value }
- cookie是Map<String,Cookie>类型,其中key是Cookie的名字,而值是Cookie对象本身。
![](https://img.haomeiwen.com/i7900193/5e1b28aa6b57696e.jpg)
- pageContext对象
pageContext是PageContext类型
可以使用pageContext对象调用getXXX()方法
例如
pageContext.getRequest(),可以${pageContext.request}。
![](https://img.haomeiwen.com/i7900193/a72453b83ca49cdb.png)
四、EL函数库
EL函数库是由第三方对EL的扩展,现在学习的EL函数库是由JSTL添加的
EL函数库就是定义一些有返回值的静态方法。然后通过EL语言来调用它们
EL函数库中包含了很多对字符串的操作方法,以及对集合对象的操作。
例如:
${fn:length(“abc”)}
会输出3,即字符串的长度
1.导函数库
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/function"%>
2.函数库
- String toUpperCase(String input):
把参数转大写 - String toLowerCase(String input):
把参数转小写 - int indexOf(String input, String substring):
从字符串中,输出小字符串的位置。 - boolean contains(String input, String substring):
查看字符串是否包含小字符串 - boolean containsIgnoreCase(String input, String substring):
忽略大小写的,是否包含 - boolean startsWith(String input, String substring):
是否以小字符串为前缀 - boolean endsWith(String input, String substring):
是否以小字符串为后缀 - String substring(String input, int beginIndex, int endIndex):
截取字串 - String substringAfter(String input, String substring)
获取大串中,小串所在位置后面的字符串
eg:
hello-world, “-“ - substringBefore(String input, String substring)
获取大串中,小串所在位置前面的字符串
eg:
hello-world, “-“ - String escapeXml(String input)
把input中“<”、">"、"&"、"'"、""",进行转义 - String trim(String input):
去除前后空格 - String replace(String input, String substringBefore, String substringAfter):
替换 - String[] split(String input, String delimiters):
分割字符串,得到字符串数组 - int length(Object obj):
可以获取字符串、数组、各种集合的长度! - String join(String array[], String separator)
联合字符串数组!
例如
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
……
String[] strs = {"a", "b","c"};
List list = new ArrayList();
list.add("a");
pageContext.setAttribute("arr", strs);
pageContext.setAttribute("list", list);
%>
${fn:length(arr) }<br/><!--3-->
${fn:length(list) }<br/><!--1-->
${fn:toLowerCase("Hello") }<br/> <!-- hello -->
${fn:toUpperCase("Hello") }<br/> <!-- HELLO -->
${fn:contains("abc", "a")}<br/><!-- true -->
${fn:containsIgnoreCase("abc", "Ab")}<br/><!-- true -->
${fn:contains(arr, "a")}<br/><!-- true -->
${fn:containsIgnoreCase(list, "A")}<br/><!-- true -->
${fn:endsWith("Hello.java", ".java")}<br/><!-- true -->
${fn:startsWith("Hello.java", "Hell")}<br/><!-- true -->
${fn:indexOf("Hello-World", "-")}<br/><!-- 5 -->
${fn:join(arr, ";")}<br/><!-- a;b;c -->
${fn:replace("Hello-World", "-", "+")}<br/><!-- Hello+World -->
${fn:join(fn:split("a;b;c;", ";"), "-")}<br/><!-- a-b-c -->
${fn:substring("0123456789", 6, 9)}<br/><!-- 678 -->
${fn:substring("0123456789", 5, -1)}<br/><!-- 56789 -->
${fn:substringAfter("Hello-World", "-")}<br/><!-- World -->
${fn:substringBefore("Hello-World", "-")}<br/><!-- Hello -->
${fn:trim(" a b c ")}<br/><!-- a b c -->
${fn:escapeXml("<html></html>")}<br/> <!-- <html></html> -->
五、自定义函数库
1.写一个类,写一个有返回值的静态方法;
2.创建demo.tld文件,可以参考fn.tld文件来写,把itcast.tld文件放到/WEB-INF目录下;
3.在页面中添加taglib指令,导入自定义标签库。
DemoFuncations.java
public class DemoFuncations {
public static String test() {
return "EL函数库测试";
}
}
demo.tld(放到classes下)
<function>
<name>test</name>
<function-class>cn.el.funcations.DemoFuncations </function-class>
<function-signature>String test()</function-signature>
</function>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="de" uri="/WEB-INF/demo.tld" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>a.jsp</h1>
${de:test() }
</body>
</html>