博客目录
- 一、EL表达式介绍
- 二、EL获取数据
- 三、EL中的内置对象
- 四、EL访问Bean的属性
- 五、EL访问数组中的数据
- 六、EL获取list中数据
- 七、EL访问Map
- 八、EL中的运算符(empty)
- 九、自定义EL函数
- 十、总结
一、EL表达式介绍
- Expression Language表达式语言
- 是一种在JSP页面获取数据的简单方式(只能获取数据,不能设置数据)
- 在JSP2.0开始引入概念
语法格式
- 在JSP页面的任何静态部分均可通过:${expression}来获取到指定表达式的值
二、EL中的内置对象
- EL有11个内置对象,这里主要讲域属性相关的4个和其他4个
- EL的11个内置对象,除了pageContext以外,其他10个内置对象的类型都是java.util.Map类型
<2.1>域属性相关(4个)
1. pageScope:从page范围域属性空间中查找指定的key
2.requestScope:从request范围域属性空间中查找指定的key
3.sessionScope:从session范围域属性空间中查找指定的key
4.applicationScope:从application范围域属性空间中查找指定的key
<%@ 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>
<%
pageContext.setAttribute("name", "linjie");
request.setAttribute("name", "lucy");
session.setAttribute("name", "king");
application.setAttribute("name", "bilibili");
%>
name=${applicationScope.name }<br>
name=${pageScope.name }<br>
name=${sessionScope.name }<br>
name=${requestScope.name }<br>
</body>
</html>
<2.2>其他重要内置对象(4个)
- 1、pageContext: 该pageContext与JSP内置对象pageContext是同一个对象。通过该对象,可以获取到request、response、session、servletContext、servletConfig等对象注意:这些对象在EL里不是内置对象,这些对象只能通过pageContext获取
- 2、param:(获取请求中的指定参数)
- 3、paramValues:获取请求中的指定参数的所以值,其底层实际调用request.getParameterValues()
- 4、initParam:获取初始化参数,其底层调用的是ServletContext.getInitParameter()
pageContext:
- 在EL中直接${pageContext.request}即可获取request对象,其底层调用的是pageContext.getRequest()方法。同理,也可以通过类似方法获取其他对象
重点:其中最常用的:${pageContext.request.contextPath },代表web应用下的根,可以看出下面action中的路径可读性更强了
package linjie.com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Regster extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<%@ 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>
<%-- ${pageContext.request.contextPath }代表web应用的根 --%>
<form action="${pageContext.request.contextPath }/regster" method="POST">
xxx<input type="text" name="name"/><br>
yyy<input type="text" name="age"/><br>
<input type="submit" value="点击">
</form>
</body>
</html>
2、param(获取请求中的指定参数)
- 其底层实际调用request.getParameter()
<%@ 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>
<%-- ${pageContext.request.contextPath }代表web应用的根 --%>
<form action="${pageContext.request.contextPath }/show.jsp" method="POST">
xxx<input type="text" name="name"/><br>
yyy<input type="text" name="age"/><br>
<input type="submit" value="点击">
</form>
</body>
</html>
<%@ 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>
name=${param.name }<br>
age=${param.age }<br>
</body>
</html>
3、paramValues
- 获取请求中的指定参数的所以值,其底层实际调用request.getParameterValues()
<%@ 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>
<%-- ${pageContext.request.contextPath }代表web应用的根 --%>
<form action="${pageContext.request.contextPath }/show.jsp" method="POST">
xxx<input type="text" name="name"/><br>
yyy<input type="text" name="age"/><br>
爱好:
<input type="checkbox" name="hobby" value="sleep">睡觉
<input type="checkbox" name="hobby" value="play">玩
<input type="checkbox" name="hobby" value="eat">吃
<input type="submit" value="点击">
</form>
</body>
</html>
<%@ 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>
name=${param.name }<br>
age=${param.age }<br>
hobby[0]=${paramValues.hobby[0] }<br>
hobby[1]=${paramValues.hobby[1] }<br>
</body>
</html>
4、initParam
- 获取初始化参数,其底层调用的是ServletContext.getInitParameter()
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>07eltttt</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--初始化参数 -->
<context-param>
<param-name>name</param-name>
<param-value>林杰</param-value>
</context-param>
<servlet>
<display-name>Regster</display-name>
<servlet-name>Regster</servlet-name>
<servlet-class>linjie.com.Regster</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Regster</servlet-name>
<url-pattern>/regster</url-pattern>
</servlet-mapping>
</web-app>
<%@ 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>
name=${initParam.name }
</body>
</html>
三、EL获取数据(从四大域中获取属性)
- EL只能从四大域中获取属性
- <3.1>.如果没有使用EL的内置对象,则查找数据顺序是依次按照由小到大范围从四大域中查找指定名称的属性值
- pageContext<request<session<application
<%@ 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>
<%
pageContext.setAttribute("name", "linjie");
request.setAttribute("name", "lucy");
session.setAttribute("name", "king");
application.setAttribute("name", "bilibili");
%>
name=${name }
</body>
</html>
可以看出没有使用EL内置对象时查找顺序是由小到大,所以最先显示的是pageContext域中的
- <3.2> 如果使用EL内置对象,从指定域中获取数据,提高了查找效率
<%@ 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>
<%
pageContext.setAttribute("name", "linjie");
request.setAttribute("name", "lucy");
session.setAttribute("name", "king");
application.setAttribute("name", "bilibili");
%>
name=${applicationScope.name }
</body>
</html>
可以看出,使用applicationScope即可指定application域中的name输出,当然其他域也是类似