大家好,又见面了,我是你们的朋友全栈君。
一、EL表达式简介EL 全名为Expression Language。EL主要作用:
1、获取数据EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web域 中检索java对象、获取数据。(某个web域 中的对象,访问javabean的属性、访问list集合、访问map集合、访问数组)
2、执行运算利用EL表达式可以在JSP页面中执行一些基本的关系运算、逻辑运算和算术运算,以在JSP页面中完成一些简单的逻辑运算。${user==null}
3、获取web开发常用对象EL 表达式定义了一些隐式对象,利用这些隐式对象,web开发人员可以很轻松获得对web常用对象的引用,从而获得这些对象中的数据。
4、调用Java方法EL表达式允许用户开发自定义EL函数,以在JSP页面中通过EL表达式调用Java类的方法。
二、EL表达式的功能1、获取数据代码语言:javascript复制 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
5
6
7
8
9
10
11
12
13
----------------获取域中存的数据-------------------------
14 <%
15 String data = "abcd"; 16 request.setAttribute("data",data); 17 %>
18 ${data } <%--pageContext.findAttribute("data") ""--%>
19
20
21
----------------获取域中存的javabean的数据-------------------------
22 <%
23 Person p = new Person(); 24 p.setName("aaaa"); 25
26 request.setAttribute("person",p); 27 %>
28 ${person.name } 29
30
31
----------------获取域中存的javabean中的javabean的数据-------------------------
32 <%
33 Person p1 = new Person(); 34 Address a = new Address(); 35 a.setCity("上海"); 36 p1.setAddress(a); 37 request.setAttribute("p1",p1); 38 %>
39 ${p1.address.city } 40
41
42
----------------获取域中存的list集合中的数据-------------------------
43 44 <% 45 List list = new ArrayList(); 46 list.add(new Person("aaa")); 47 list.add(new Person("bbb")); 48 request.setAttribute("list",list); 49 %> 50 ${list['1'].name } 51
52
53
----------------获取域中map集合中的数据-------------------------
54 <% 55 Map map = new HashMap(); 56 map.put("aa",new Person("aaaaa")); 57 map.put("bb",new Person("bbbbb")); 58 map.put("cc",new Person("ccccc")); 59 map.put("dd",new Person("ddddd")); 60 map.put("111",new Person("eeeee")); 61 request.setAttribute("map111",map); 62 %> 63 ${map111.cc.name } 64
65
66
67
----------------其它几个常用的el表达式-------------------------
68 ${pageContext.request.contextPath } 69 点点
70
71
72
73 EL表达式语句在执行时,会调用pageContext.findAttribute方法,用标识符为关键字,分别从page、request、session、application四个域中查找相应的对象,找到则返回相应对象,找不到则返回”” (注意,不是null,而是空字符串)。
2、执行运算语法:${运算表达式},EL表达式支持如下运算符:
empty运算符:检查对象是否为null或“空”
二元表达式:${user!=null?user.name : “”}
[ ] 和 . 号运算符
代码语言:javascript复制 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2
3
4
5
6
7
8
9
10
11 <%
12 request.setAttribute("username","aaa"); 13 request.setAttribute("password","124"); 14 %>
15
16 ${username=='aaa' && password=='123' } 17
18
----------------empty运算符-------------------------
19
20 <% 21 //request.setAttribute("list",null); 22 request.setAttribute("list",new ArrayList()); 23 %> 24 ${empty(list) } 25
26
----------------二元运算符-------------------------
27 ${user!=null ? user.username : '' } 28
29
30
----------------二元运算符(数据回显)-------------------------
31
32 <% 33 request.setAttribute("gender","male"); 34 %> 35 36 男 37 女
49
50 3、获取web开发常用对象EL表达式语言中定义了11个隐含对象,使用这些隐含对象可以很方便地获取web开发中的一些常见对象,并读取这些对象的数据。
语法:${隐式对象名称} :获得对象的引用。
代码语言:javascript复制 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2
3
4
5
6
7
8
9
10
11 ${pageContext} 12
15
----------------使用el隐式对象访问指定的域-------------------------
16 <%
17 pageContext.setAttribute("aa","123"); 18 %>
19 ${aa } 20 ${pageScope.aa } 21
22 ${sessionScope.user!=null } 23
24
25
26
----------------获取保存了所有请求参数的Map对象-------------------------
27 ${param.name } 28
29 ${paramValues.name[0] } 30 ${paramValues.name[1] } 31
32
33
34
----------------获取请求头-------------------------
35 ${header['Accept-Language'] }代码语言:javascript复制36
37
---------------获取cookie-------------------------
38 ${cookie.JSESSIONID.name }
39 ${cookie.JSESSIONID.value } 40
41
42 ${initParam.xx } 43
44 重要应用:
可以从指定的域中直接取数据,提高查找效率
param隐含对象用于数据回显,之前的做法是把表单提交的数据封装到javabean,在服务端进行校验,如果校验失败就把bean存到request域中,再从页面中从request取出来回显
三、使用EL调用java方法EL表达式语法允许开发人员开发自定义函数,以调用Java类的方法。
示例:${prefix:method(params)}
在EL表达式中调用的只能是Java类的静态方法。
这个Java类的静态方法需要在TLD文件中描述,才可以被EL表达式调用。
EL自定义函数用于扩展EL表达式的功能,可以让EL表达式完成普通Java程序代码所能完成的功能
1、EL Function开发步骤一般来说, EL自定义函数开发与应用包括以下三个步骤:
编写一个Java类的静态方法
编写标签库描述符(tld)文件,在tld文件中描述自定义函数。
在JSP页面中导入和使用自定义函数
实例:开发对html标签进行转义的EL函数
java类的静态方法:
代码语言:javascript复制 1 public class MyEL { 2 //进行html标签转义
3 public static String filter(String message) { 4
5 if (message == null) 6 return (null); 7
8 char content[] = new char[message.length()]; 9 message.getChars(0, message.length(), content, 0); 10 StringBuffer result = new StringBuffer(content.length + 50); 11 for (int i = 0; i < content.length; i++) { 12 switch (content[i]) { 13 case '<': 14 result.append("<"); 15 break; 16 case '>': 17 result.append(">"); 18 break; 19 case '&': 20 result.append("&"); 21 break; 22 case '"': 23 result.append("""); 24 break; 25 default: 26 result.append(content[i]); 27 } 28 } 29 return (result.toString()); 30
31 } 32
33 //进行两个字符串连接
34 public static String add(String s1,String s2){ 35 return s1 + s2; 36 } 37 }标签库描述文件:
代码语言:javascript复制 1
2
18
19 20 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 21 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 22 version="2.0"> 23 24 25 26 27 28 39
代码语言:javascript复制 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <%@taglib uri="/my" prefix="my" %>
3
4
5
6
7
8
9
10 ${my:filter("点点") } 11
12 2、开发EL函数注意事项 编写完标签库描述文件后,需要将它放置到
TLD文件中的
EL表达式保留关键字
所谓保留字的意思是指变量在命名时,应该避开上述的名字,以免程序编译时发生错误。
四、sun公司的el函数库1、el函数库所有标签描述代码语言:javascript复制 1
2
3 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 6 version="2.0"> 7 8 9 10 11 12 13 14 15 16 Tests if an input string contains the specified substring. 17 18 19 20 21 22 <c:if test="${fn:contains(name, searchString)}"> 23 24 25 26 27 28 Tests if an input string contains the specified substring in a case insensitive way. 29 30 31 32 33 34 <c:if test="${fn:containsIgnoreCase(name, searchString)}"> 35 36 37 38 39 40 Tests if an input string ends with the specified suffix. 41 42 43 44 45 46 <c:if test="${fn:endsWith(filename, ".txt")}"> 47 48 49 50 51 52 Escapes characters that could be interpreted as XML markup. 53 54 55 56 57 58 ${fn:escapeXml(param:info)} 59 60 61 62 63 64 Returns the index withing a string of the first occurrence of a specified substring. 65 66 67 68 69 70 ${fn:indexOf(name, "-")} 71 72 73 74 75 76 Joins all elements of an array into a string. 77 78 79 80 81 82 ${fn:join(array, ";")} 83 84 85 86 87 88 Returns the number of items in a collection, or the number of characters in a string. 89 90 91 92 93 94 You have ${fn:length(shoppingCart.products)} in your shopping cart. 95 96 97 98 99 100 Returns a string resulting from replacing in an input string all occurrences 101 of a "before" string into an "after" substring. 102 103 104 105 106 107 ${fn:replace(text, "-", "")} 108 109 110 111 112 113 Splits a string into an array of substrings. 114 115 116 117 118 119 ${fn:split(customerNames, ";")} 120 121 122 123 124 125 Tests if an input string starts with the specified prefix. 126 127 128 129 130 131 <c:if test="${fn:startsWith(product.id, "100-")}"> 132 133 134 135 136 137 Returns a subset of a string. 138 139 140 141 142 143 P.O. Box: ${fn:substring(zip, 6, -1)} 144 145 146 147 148 149 Returns a subset of a string following a specific substring. 150 151 152 153 154 155 P.O. Box: ${fn:substringAfter(zip, "-")} 156 157 158 159 160 161 Returns a subset of a string before a specific substring. 162 163 164 165 166 167 Zip (without P.O. Box): ${fn:substringBefore(zip, "-")} 168 169 170 171 172 173 Converts all of the characters of a string to lower case. 174 175 176 177 178 179 Product name: ${fn.toLowerCase(product.name)} 180 181 182 183 184 185 Converts all of the characters of a string to upper case. 186 187 188 189 190 191 Product name: ${fn.UpperCase(product.name)} 192 193 194 195 196 197 Removes white spaces from both ends of a string. 198 199 200 201 202 203 Name: ${fn.trim(name)} 204 205 206 207
2 <%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 3 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
4
5
6
7
8
9
10
11
12
13
14 ${fn:toLowerCase("AAAA") } 15
16 <%
17 request.setAttribute("arr",new String[5]); 18 %>
19
20 ${fn:length(arr) } 21
22
23 <%
24 List list = new ArrayList(); 25 list.add("aa"); 26 list.add("bb"); 27 request.setAttribute("list",list); 28 %>
29
32
33
34 ${fn:join(fn:split("www,itcast,cn",","),".") } 35
36 ${fn:contains("aaaabbbcc","ab") } 37
38
39 ${fn:escapeXml("点点")} 40
41
42
---------------取出用户填写的爱好回显------------------------
43 <% 44 request.setAttribute("likes",new String[]{"football","sing"}); 45 %> 46 47 唱歌 48 跳舞 49 蓝球 50 足球 51
52 注意:el表达式不支持两个字符串的连接,如:
代码语言:javascript复制 1
2
3 <%
4 User user = new User(); 5 user.setUsername("aaaa"); 6 //session.setAttribute("user",user); 7 %>
8
9 ${user!=null?"欢迎您"+user.name:""} 10
11 要想进行两个字符串连接可以开发el函数实现,如:
代码语言:javascript复制1 //进行两个字符串连接
2 public static String add(String s1,String s2){ 3 return s1 + s2; 4 }在tld文件描述后再使用
代码语言:javascript复制 1
2
3 <%
4 User user = new User(); 5 user.setUsername("aaaa"); 6 //session.setAttribute("user",user); 7 %>
8
9 ${user!=null?my:add("欢迎您:",user.username):''} 10
11 发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/155095.html原文链接:https://javaforall.cn