这篇来学习下EL,了解为什么JSP中为什么要使用EL?EL表达式:expression language 表达式语言,用来简化jsp中java代码开发。它不是一种开发语言,是jsp中获取数据一种规范。
1.EL获取数据举例
场景:有两个jsp文件,分别是1.jsp和2.jsp, 我们在1.jsp中写代码,请求转发到2.jsp,然后再2.jsp中写获取属性值,打印到页面上。
1.jsp代码
<%@page import="com.anthony.entity.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Student s = new Student();
s.setName("Anthony");
//把s对象装到 request域对象中
request.setAttribute("s", s);
//请求转发到2.jsp
request.getRequestDispatcher("/2.jsp").forward(request, response);
%>
</body>
</html>
2.jsp代码
根据我们前面学习到的知识,我们写获取Student的name是需要写在<%%>中,我们也先来这样做。
<%@page import="com.anthony.entity.Student"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Student s = (Student)request.getAttribute("s");
out.print(s.getName());
%>
</body>
</html>
运行效果,确实能拿到name的值
我们使用EL表达式来简化2.jsp中获取name的代码。
<%@page import="com.anthony.entity.Student"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${s.name}
</body>
</html>
解释:
${s} 等价于 Student s = new Student(); ${s.name}等价于 去Student.java找到getName()方法。
原理就是,这里调用了PageContext.findAttribute("s"), 这个就是底层代码实现。前面我们知道了这个findAttribute()方法会依次在四个域对象中查找属性名称为s的值。因为我们在1.jsp中通过request域对象设置了s这个属性,所以这里能得到s对象。
我们管s.name这种语法叫做 属性导航, 下面再举例一个来演示属性导航。
首先,我们在Student.java中添加一个City属性,City是一个自定义类对象。
package com.anthony.entity;
public class City {
private String address = "Beijing";
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.anthony.entity;
public class Student {
private String name;
private String pwd;
private City city = new City();
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
这个时候,1.jsp内容不变,2.jsp代码如下
<%@page import="com.anthony.entity.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${s.city.address}
</body>
</html>
这个就是属性导航体现,先找到s对象,然后getCity()得到city属性,然后通过City.java中getAddress()方法找到address的值。
2.EL获取空对象不会产生空指针异常
这里假如我们把2.jsp代码改成这样,由于ss我们没有在1.jsp中写入域对象,得到会是一个空对象,会发送空指针异常。
<%@page import="com.anthony.entity.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Student s = (Student)request.getAttribute("ss");
out.print(s.getName());
%>
</body>
</html>
部署之后,访问1.jsp。出现空指针异常。
如果我们使用EL,这里就用户看不到控制针异常,而是一个空白页面。
<%@page import="com.anthony.entity.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${ss.name}
</body>
</html>
访问出现空白页面,EL中得到name底层给转换成了一个空字符串。
3.EL中[]运算符
上面我们获取数据使用了点(.),这里我们来学习一个[]运算符。[]运算符特点是:点能做到的,[]也能做到,[]能做到的,点不一定能做到。这里把2.jsp改成[]的方式获取数据,看看效果。
<%@page import="com.anthony.entity.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${s["name"]}
${s.city["address"]}
</body>
</html>
举例一个list的取值
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
request.setAttribute("list", list);
%>
${list[1]}
</body>
</html>
得到
在举例一个Map集合
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Map<String, String> m = new HashMap<String, String>();
m.put("a", "aaa");
m.put("b", "bbb");
m.put("c", "ccc");
request.setAttribute("map", m);
%>
${map['a']}
</body>
</html>
得到
总结: EL表达式主要作用就是用来获取变量并输出。