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类的方法。
二、实例
1、获取数据
使用EL表达式获取数据语法:"${标识符}"
EL表达式语句在执行时,会调用pageContext.findAttribute方法,用标识符为关键字,分别从page、request、session、application四个域中查找相应的对象,找到则返回相应对象,找不到则返回”” (注意,不是null,而是空字符串)。
EL表达式可以很轻松获取JavaBean的属性,或获取数组、Collection、Map类型集合的数据
例:
<body>
<%
//将对象设置到pagecontext域对象里
User u=new User("tom",20);
pageContext.setAttribute("user", u);
//将集合设置到request域对象里
ArrayList<User> list=new ArrayList<User>();
list.add(u);
request.setAttribute("list", list);
//设置map集合到pageContext域对象里
Map<String,User> m=new HashMap<String,User>();
m.put("1", u);
m.put("2",new User("jia",22));
pageContext.setAttribute("map", m);
int []num={1,2,3,4,5};
pageContext.setAttribute("array", num);
%>
获取key为user的对象:${user}<br/>
获取该对象的name:${user.name}<br/>
获取数组:${array[0]}<br/>
获取list集合:${list}<br/>
获取list集合的第一个元素的age属性${list[0].age}<br/>
获取key为map的map集合:${map}<br/>
获取key为1的对象的age属性${map["1"].age}
</body>
注意:在不同的域有相同的属性名时,会根据从域中按照page、request、session、application的顺序查找
<%
session.setAttribute("name", "tom");
request.setAttribute("name", "jia");
%>
<
h1>${name}</
h1>//jia
如果要输出
tom需要这样写:
<
h1>${sessionScope.name}</
h1>
效果
<body>
<%
request.setAttribute("name","tom");
%>
<%--${name}等同于pageContext.findAttribute("name") --%>
使用EL表达式获取数据:${name}
<hr>
<!-- 在jsp页面中,使用el表达式可以获取bean的属性 -->
<%
Person p = new Person();
p.setAge(12);
request.setAttribute("person",p);
%>
使用el表达式可以获取bean的属性:${person.age}
<hr>
<!-- 在jsp页面中,使用el表达式可以获取bean中的属性 -->
<%
Person person = new Person();
Address address = new Address();
person.setAddress(address);
request.setAttribute("person",person);
%>
${person.address.name}
<hr>
<!-- 在jsp页面中,使用el表达式获取list集合中指定位置的数据 -->
<%
Person p1 = new Person();
p1.setName("tom");
Person p2 = new Person();
p2.setName("tom");
List<Person> list = new ArrayList<Person>();
list.add(p1);
list.add(p2);
request.setAttribute("list",list);
%>
<!-- 取list指定位置的数据 -->
${list[1].name}
<!-- 迭代List集合 -->
<c:forEach var="person" items="${list}">
${person.name}
</c:forEach>
<hr>
<!-- 在jsp页面中,使用el表达式获取map集合的数据 -->
<%
Map<String,String> map = new LinkedHashMap<String,String>();
map.put("a","a");
map.put("b","b");
map.put("c","c");
map.put("1","a");
request.setAttribute("map",map);
%>
<!-- 根据关键字取map集合的数据 -->
${map.c}
${map["1"]}
<hr>
<!-- 迭代Map集合 -->
<c:forEach var="me" items="${map}">
${me.key}=${me.value}<br/>
</c:forEach>
<hr>
</body>
</html>
2、执行运算
<%@page import="com.web.el.User"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="
http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>el表达式运算符</title>
</head>
<body>
<h3>el表达式进行四则运算:</h3>
加法运算:${5+1}//不能拼接成字符串只能相加${"5"+1}
<br /> 减法运算:${1-1}
<br /> 乘法运算:${1*4}
<br /> 除法运算:${10/3}
<br />
<h3>el表达式进行关系运算:</h3>
<%--${user == null}和 ${user eq null}两种写法等价--%>
${user == null}
<br /> ${user eq null}
<br />
<h3>el表达式使用empty运算符检查对象是否为null(空)</h3>
<%
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
request.setAttribute("list", list);
%>
<%--使用empty运算符检查对象是否为null(空) --%>
<c:if test="${!empty(list)}">
<c:forEach var="str" items="${list}">
${str}<br />
</c:forEach>
</c:if>
<br />
<%
List<String> emptyList = null;
%>
<%--使用empty运算符检查对象是否为null(空) --%>
<c:if test="${empty(emptyList)}">
这是一个空的list集合
</c:if>
<br />
<h3>EL表达式中使用二元表达式</h3>
<%
session.setAttribute("user", new User("jia", 20));
%>
${user==null? "对不起,您没有登陆 " : user.name}
<br />
<h3>EL表达式数据回显</h3>
<%
User u = new User();
u.setGender("male");
//数据回显
request.setAttribute("user", u);
%>
<input type="radio" name="gender" value="male"
${u.gender=='male'?'checked ':''}>男
<input type="radio" name="gender" value="female"
${u.gender=='female'?'':''} >女
<br />
</body>
</html>
三、内置对象
PageContext pageContext | 是JSP页面的上下文对象,代表了JSP页面 |
Map pageScope | 封装了pageContext域对象的数据 |
Map requestScope | 封装了request域对象的数据 |
Map sessionScope | 封装了session域对象的数据 |
Map applicationScope | 封装了application域对象的数据 |
Map param | 封装了表单提交的数据,Map的value类型是String |
Map paramValues | 封装了表单提交的数据,Map的value类型是String |
Map header | 封装了请求头,Map的value类型是String |
Map headerValues | 封装了请求头,Map的value类型是String[] |
Map cookie | 封装了客户端带来的cookie |
Map initParam | 封装了WEB应用的初始化参数 |
四、内置对象的使用
1、获取路径
${pageContext.request.contextPath}
2、获取key为username cookie的值
${cookie.username.value}
3、获取请求参数
${param.usernmae}