博客目錄
- 一、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輸出,當然其他域也是類似