EL

一、EL操作符
语法:EL1.访{EL表达式} 1 “.”操作符 用来访问对象的某个属性,如{user.name}。

“[ ]”操作符
也可用来访问对象的某个属性,如${user[‘name’]}。

“[ ]”操作符其他功能:

(1)当属性名中包含特殊字符如“.”或“-”等的情况下,就不能使用“.”操作符来访问,这时只能使用“[ ]”操作符。

(2)根据索引值访问数组中的元素使用“[ ]”操作符,如${arr[1]}。

(3)使用变量实现动态访问,如${user[x]},x是一个变量,改变x即可。

1.1、算术操作符
这里只测试了“+”操作符,“-”、“*”、“/”或div、“%”或mod请自行测试。

${a1+a2+a3 }

${1+1 }

${‘1’+1.1 }

${“1”+1 }

${bbbbbsdcksnvjnds+1 }

${“bbbbbsdcksnvjnds”+1 }
1.2、关系操作符
EL

1.3、逻辑操作符

EL
1.4、empty操作符

${empty s }

${!empty s }

${not empty s }

${!not empty s }

总结:若变量 s为null,或长度为零的String,或size为零的集合,则
${ empty s }返回的结果为true,
notemptys{ not empty s }或{ ! empty s }返回的结果为false。

二、EL内置隐式对象
2.1、EL内置对象之域对象
<%
pageContext.setAttribute(“a”, “1”);
request.setAttribute(“a”, “2”);
session.setAttribute(“a”, “3”);
application.setAttribute(“a”, “4”);
%>

${pageScope.a }
${requestScope.a }
${sessionScope.a }
${applicationScope.a }

${a }

【总结】:${a }底层使用pageContext.findAttribute(“a”),依次从page、request、session、application的域范围中寻找”a”属性,如果都没有则返回null 。

2.2、EL内置对象之param、paramValues(请求参数对象)

${param.name }

${param.hobby }

${paramValues.name }

${paramValues.name[0] }

${paramValues.hobby }

${paramValues.hobby[1] }

【总结】:
${param.xxx } 相当于reuqest.getParameter(“xxx”);
${paramValues.xxx } 相当于reuqest.getParameterValues(“xxx”);
如果是数组可以使用下标。

2.3、EL内置对象之header、headerValues(请求头对象)
${header.accept }

${header.accept-Encoding }

${header[‘accept-Encoding’] }

${headerValues[‘accept-Encoding’] }

${headerValues[‘accept-Encoding’][0] }

2.4、EL内置对象之pageContext对象
${pageContext.request.contextPath }

1
【总结】:pageContext对象:封装了其他8个JSP内置对象,可以任意获取使用。

2.5、EL内置对象之cookie对象

${cookie }

2.6、EL内置对象之initParam(全局初始化参数对象)

${initParam.Encoding }

${initParam }

三、EL获取数据实例
3.1、EL获取域中属性
<%
String str = “hello”;
pageContext.setAttribute(“str”, str);
%>
${pageScope.str }

${str }

3.2、EL获取数组元素
<%
String[] s = {“zhang”,“xiao”,“hu”};
pageContext.setAttribute(“s”, s);
%>
${pageScope.s }

${pageScope.s[2] }

3.3、EL获取 List 中的数据
<%
List list = new ArrayList();
list.add(“zhang”);
list.add(“xiao”);
list.add(“hu”);
pageContext.setAttribute(“list”, list);
%>
${list }

${list[1] }

3.4、EL获取 Map<String, Integer> 中的数据
<%
Map<String, Integer> map = new HashMap<String, Integer>();
map.put(“zhang”,1);
map.put(“xiao”,2);
map.put(“hu”,3);
pageContext.setAttribute(“map”, map);
%>
${map }

${map.zhang }

3.5、EL获取 Map<String, JavaBean> 中的数据
<%
Map<String, User> umap = new HashMap<String, User>();
umap.put(“user1”, new User(“zhang”,18));
umap.put(“user2”, new User(“xiao”,36));
pageContext.setAttribute(“umap”, umap);
%>
${umap }

${umap.user2 }

${umap.user1.age }

${umap[‘user2’].name }

${umap[‘user2’][‘name’] }

${umap[‘user2’][‘age’] }

${umap[‘user2’][age] }

相关文章: