Web階段:第十三章:EL表達式
什么是EL表達式?
E L的全稱:Expression Language,就是表達式語言??梢暂敵霰磉_式的值。跟jsp的表達式腳本一樣。計算表達式的值后輸出。 EL表達式出現(xiàn)的目的是為了使JSP寫起來更加簡單,讓jsp的代碼更佳簡化。
我們先來看一下EL表達式的一個Hello world 程序,看看它是如何簡化jsp代碼。
EL 表達式的Hello world 程序?。?!
- <%@ 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>
- <%
- //首先我們需要在request域?qū)ο笾性O(shè)置一個屬性
- request.setAttribute("hello", "這是內(nèi)容");
- %>
- <%-- 獲取請求域中的屬性hello輸出 --%>
- jsp的輸出:<%=request.getAttribute("hello") == null ? "" : request.getAttribute("hello")%><br/><br/>
- <%-- 輸出在域中查找輸出hello的值 --%>
- EL表達式的輸出:${hello}<br/><br/>
- </body>
- </html>
從上面的程序,我們不難看出。我們要輸出域中的屬性,方便多了。
所以el表達式使得jsp頁面的代碼變得更加簡潔。主要用于替換 jsp 中表達式腳本。
EL表達式的最主要功能就是從域?qū)ο笾蝎@取數(shù)據(jù),并且輸出
EL表達式,獲取域?qū)ο髷?shù)據(jù)(*****重點)
使用EL表達式獲取數(shù)據(jù)的語法: “${標識符}”
第一點:當EL表達式輸出的key不存在的時候,輸出的是空串””
第二點:EL表達式在域?qū)ο笾兴阉鲗傩缘捻樞蚴撬阉魉膫€域?qū)ο蟮捻樞?
是從小到大,pageContext=====>>>>
request=====>>>>session=====>>>>application
EL表達式可以從域?qū)ο笾蝎@取數(shù)據(jù)
1、EL表達式獲取域數(shù)據(jù)的順序
EL 表達式語句在執(zhí)行時,會用標識符為關(guān)鍵字分別從page、request、session、application四個域中查找對應(yīng)key的對象。
找到則返回相應(yīng)數(shù)據(jù)。找不到則返回空串。(注意,不是null,而是空字符串)
- <%@ 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>
- <%
- //我們在四個域?qū)ο笾性O(shè)置同一個屬性
- pageContext.setAttribute("hello", "page這是內(nèi)容");
- request.setAttribute("hello", "request這是內(nèi)容");
- session.setAttribute("hello", "session這是內(nèi)容");
- application.setAttribute("hello", "application這是內(nèi)容");
- %>
- <%-- 從page,request,session,application四個作用域中順序查找對應(yīng)的key的值輸出,如果沒有找到就輸出空串 --%>
- ${hello}<br/>
- </body>
- </html>
測試步驟:
1.寫好頁面代碼,直接訪問頁面,輸出pageContext中的內(nèi)容。
2.注掉pageContext.setAttribute 代碼。刷新頁面,輸出request域范圍的hello屬性值。
3.注掉 request.setAttribute 代碼。刷新頁面,輸出Session域范圍的hello屬性值。
4.注掉 session.setAttribute 代碼,并且關(guān)閉瀏覽器后重新打開瀏覽器訪問頁面。輸出 application(ServletContext)域范圍的屬性值
5.注掉application.setAttribute代碼,關(guān)閉服務(wù)器。然后再啟動服務(wù)器。再打開瀏覽器,再訪問頁面。application中也沒有數(shù)據(jù)了
2、獲取javaBean普通屬性、數(shù)組屬性、List集合屬性,以map屬性中的數(shù)據(jù)。
例如:
${ user.username } // 獲取user對象中。username屬性值
${ list[下標] } // 訪問有序集合(或數(shù)組)中給定索引的元素
${ map.key } // 訪問map集合中指定key的屬性值
${ map[“key”] } // 訪問特殊字符串的key的屬性值
注意:[] 中括號 除了可以訪問帶有順序的集合和數(shù)組的元素之外。
還可以訪問特殊的key值
需求:創(chuàng)建一個User類對象,添加字符串屬性,數(shù)組屬性,List集合屬性。map屬性。
然后創(chuàng)建一個對象實例添加到request域?qū)ο笾袦y試獲取
一定要記住一點,EL表達式獲取數(shù)據(jù)的時候,是通過對應(yīng)的get方法獲取的
BeanUtils 是通過set方法設(shè)置值
a) 先定義一個JavaBean對象------User類
public class User {
private String username;
private String[] phones;
private Map<String, Object> map;
private List<String> strList;
b) 在jsp頁面中添加一些對象到四個域?qū)ο笾?,使用el表達式訪問測試。
- <%@page import="java.util.ArrayList"%>
- <%@page import="java.util.List"%>
- <%@page import="java.util.HashMap"%>
- <%@page import="com.atguigu.servlet.User"%>
- <%@ 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>
- <%
- // 創(chuàng)建一個map對象,并添加數(shù)據(jù)
- HashMap<String,Object> map = new HashMap<String,Object>();
- map.put("aaa", "AAAValue");
- map.put("bbb", "bbbValue");
- map.put("ccc", "cccValue");
- map.put("ddd", "dddValue");
-
- // 創(chuàng)建一個list集合對象
- List<String> strList = new ArrayList<String>();
- strList.add("aaa");
- strList.add("bbb");
- strList.add("ccc");
-
- // 創(chuàng)建一個User對象
- User user = new User("用戶名",new String[]{"第一個電話","第二個電話","第三個電話"},map,strList);
-
- // 把用戶對象添加到請求request的屬性中
- request.setAttribute("user", user);
- %>
- <%--訪問 對象的username 屬性--%>
- user對象的username屬性值---->>>>${ user.username }<br/><br/>
-
- <%--訪問user對象中數(shù)組的第二個元素--%>
- user對象中phones數(shù)組的第二個元素-------->>>>${ user.phones[1] }<br/><br/>
-
- <%--訪問 list集合 中第一個元素--%>
- list集合中第一個元素-------->>>>>${ user.strList[0] }<br/> <br/>
-
- <%--訪問 user對象中map集合的aaa的屬性值 --%>
- user對象中map集合中aaa屬性的值----->>>>>${ user.map.aaa }<br/><br/>
-
- </body>
- </html>
頁面輸出如下:
注意:去掉多余的代碼驗證
EL 表達式–運算。
語法:${ 運算表達式 } , EL 表達式支持如下運算符:
1)關(guān)系運算
2)邏輯運算
3)算數(shù)運算
4)empty運算符(***常用)
empty 運算可以判斷el表達式的某個key的值是否為空,如果為空返回true,反之就返回false。
1、值為null的時候。返回true
2、值為空串的時候,返回true
3、值為Object數(shù)組時,并且長度為零,返回true
4、值為list集合,然后元素個數(shù)為零,返回true
5、值為map集合,然后元素個數(shù)為零,返回true
empty運算測試代碼:
- <%@page import="java.util.Map"%>
- <%@page import="java.util.List"%>
- <%@page import="java.util.HashMap"%>
- <%@page import="java.util.ArrayList"%>
- <%@ 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>
- <%
- // 對象為null時,返回true
- request.setAttribute("nullObject", null);
- //如果是空的字符串,返回true
- request.setAttribute("emptyStr", "");
- //如果是空的數(shù)組,返回true
- request.setAttribute("emptyArr", new Object[]{});
- //空的集合,返回true
- List list = new ArrayList();
- request.setAttribute("emptyList", list);
- //空的map,返回true
- Map map = new HashMap();
- request.setAttribute("emptyMap", map);
- %>
- <h1>EL empty 運算</h1>
- 對象為null,empty為真 ---->>>>${ empty nullObject }<br/>
- 空的字符串,empty為真------>>>>${ empty emptyStr }<br/>
- 空的數(shù)組,empty為真------>>>>${ empty emptyArr }<br/>
- 空的list集合,empty為真---->>>>${ empty emptyList }<br/>
- 空的map集合,empty為真----->>>>${ empty emptyMap }<br/>
- </body>
- </html>
瀏覽器運行結(jié)果:
5)三元運算
我們可以在EL 表達式中方便的使用三元運算輸出。
語法:${ 表達式1 ? 表達式2 : 表達式3 }
示例:${ 12 == 12 ? "12 等于 12" : "12 != 12" }
我們可以很方便的在EL 表達式中使用三元運算符進行運算。
${ 表達式1 ? 表達式2:表達式3 }
當表達式1值為真時,EL輸出表達式2的值
當表達式1值為假時,EL輸出表達式3的值
6)“.” 點 和 [] 中括號 運算符
“.” 運算符,可以取JavaBean對象的屬性值,也可以取map中某個key的值。
[] 中括號,可以獲取有序集合中指定索引的元素,也可以獲取特殊key的值。
當我們在map對象中存放一些特殊的key的時候。
比如說。key字符串中含有 “.” 、“+” 、“-” 、“*” 、“/” 、 “%” 等 這些運算符的時候。
會讓el解析器產(chǎn)生歧義的時候。我們可以使用[‘key’]中括號加引號包含key的形式取值。
[] 中括號,不僅可以獲取有序集合(數(shù)組和List集合)中的給定索引的元素,
還可以獲取key中含有特殊意義字符的key對應(yīng)的值。
比如key中含有 “.” , “+” , “_” , “*” , “/” 等的運算字符
示例:
- <body>
- <%
- //設(shè)置
- Map map = new HashMap();
- map.put("a-b-c","a-b-c-Value");
- map.put("a.b.c", "a.b.c.Value");
- map.put("aaa","aaa-Value");
- request.setAttribute("map", map);
- %>
- <%-- 下面我們可以通過中括號方式獲取對應(yīng)key的值 --%>
- ${ map['a.b.c'] }<br/>
- ${ map['a-b-c'] }<br/>
- ${ map.aaa }<br/>
- </body>
輸出為:
a.b.c.Value
a-b-c-Value
aaa-Value
EL表達式中11個隱含對象。
EL表達式 中隱含11個對象,這11個對象我們都可以直接使用!??!
EL表達式獲取域?qū)ο笾械臄?shù)據(jù)(****重點)
pageScope <=== 對應(yīng) ===> pageContext 域中的屬性
requestScope <=== 對應(yīng) ===> request 域中的屬性
sessionScope <=== 對應(yīng) ===> session 域中的屬性
applicationScope <=== 對應(yīng) ===> ServletContext 域中的屬性
我們先來看一下。如何從四個域?qū)ο笾?。獲取各自的屬性
需求:分別往四個域?qū)ο笾写鎯?shù)據(jù),然后使用pageScope,requestScope,sessionScope,applicationScope中取出數(shù)據(jù)
- <%@ 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>
- <%
- // 在四個域中存放數(shù)據(jù),進行獲取
- pageContext.setAttribute("key", "pageContext-value");
- request.setAttribute("key", "request-Value");
- session.setAttribute("key", "session-value");
- application.setAttribute("key", "application-value");
- %>
- <%-- 從不同的域中獲取數(shù)據(jù) --%>
- page域中key的值:${ pageScope.key }<br/>
- request域中key的值:${ requestScope.key }<br/>
- session域中key的值:${ sessionScope.key }<br/>
- application域中key的值:${ applicationScope.key }<br/>
- </body>
- </html>
運行的結(jié)果:
pageContext訪問Jsp中內(nèi)置對象(用的不多)。
通過pageContext對象。我們可以直接獲取jsp中的一些內(nèi)置對象,
比如:
request對象,
session對象,
Servletconfig對象,
ServletContext對象,
然后獲取一些我們需要的信息。
常用的功能獲取
協(xié)議:
服務(wù)器ip:
服務(wù)器端口:
獲取工程路徑:
獲取請求方法:
獲取客戶端ip地址:
獲取會話的id編號:
pageContext使用示例代碼
- <%@ 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="pragma" content="no-cache" />
- <meta http-equiv="cache-control" content="no-cache" />
- <meta http-equiv="Expires" content="0" />
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- 協(xié)議:${ pageContext.request.scheme }<br/>
- 服務(wù)器ip:${ pageContext.request.serverName }<br/>
- 服務(wù)器端口:${ pageContext.request.serverPort }<br/>
- 獲取工程路徑:${ pageContext.request.contextPath }<br/>
- 獲取請求方法:${ pageContext.request.method }<br/>
- 獲取客戶端ip地址:${ pageContext.request.remoteHost }<br/>
- 獲取會話的id編號:${ pageContext.session.id }<br/>
- </body>
- </html>
pageContext對象最常用的功能就是獲取上下文路徑(也就是工程路徑名)
工程名(上下文路徑):\${ pageContext.request.contextPath }
但是在實際項目中。為了縮短代碼量,會把request對象放在pageContext域?qū)ο笾小H缓笤偈褂?,比如說
<%
// 先把request對象放到pageContext域?qū)ο笾?br> pageContext.setAttribute(“req”,request);
%>
然后EL表達式代碼改為
工程名(上下文路徑):\${ req.contextPath }
EL表達式其他隱含對象的使用。
web.xml文件中的配置內(nèi)容:
- <context-param>
- <param-name>username</param-name>
- <param-value>root</param-value>
- </context-param>
使用的示例代碼:
- <%@ 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>
- 參數(shù)username的值:${ param.username }<br/>
- 參數(shù)hobby的值:${ paramValues.hobby[0] }<br/>
- 請求頭Accept-Language的值:${ header["Accept-Language"] }<br/>
- 請求頭Accept的值:${ headerValues["Accept"][0] }<br/>
- cookie的key = ${ cookie.JSESSIONID.name } : value = ${ cookie.JSESSIONID.value } <br/>
- 上下文參數(shù):${ initParam.username }<br/>
- </body>
- </html>
訪問的顯示結(jié)果: