Web階段:第十三章:EL表達式

什么是EL表達式?
E L的全稱:Expression Language,就是表達式語言??梢暂敵霰磉_式的值。跟jsp的表達式腳本一樣。計算表達式的值后輸出。 EL表達式出現(xiàn)的目的是為了使JSP寫起來更加簡單,讓jsp的代碼更佳簡化。

我們先來看一下EL表達式的一個Hello world 程序,看看它是如何簡化jsp代碼。
EL 表達式的Hello world 程序?。?!

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <%
  11. //首先我們需要在request域?qū)ο笾性O(shè)置一個屬性
  12. request.setAttribute("hello", "這是內(nèi)容");
  13. %>
  14. <%-- 獲取請求域中的屬性hello輸出 --%>
  15. jsp的輸出:<%=request.getAttribute("hello") == null ? "" : request.getAttribute("hello")%><br/><br/>
  16. <%-- 輸出在域中查找輸出hello的值 --%>
  17. EL表達式的輸出:${hello}<br/><br/>
  18. </body>
  19. </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,而是空字符串)

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <%
  11. //我們在四個域?qū)ο笾性O(shè)置同一個屬性
  12. pageContext.setAttribute("hello", "page這是內(nèi)容");
  13. request.setAttribute("hello", "request這是內(nèi)容");
  14. session.setAttribute("hello", "session這是內(nèi)容");
  15. application.setAttribute("hello", "application這是內(nèi)容");
  16. %>
  17. <%-- 從page,request,session,application四個作用域中順序查找對應(yīng)的key的值輸出,如果沒有找到就輸出空串 --%>
  18. ${hello}<br/>
  19. </body>
  20. </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表達式訪問測試。

  1. <%@page import="java.util.ArrayList"%>
  2. <%@page import="java.util.List"%>
  3. <%@page import="java.util.HashMap"%>
  4. <%@page import="com.atguigu.servlet.User"%>
  5. <%@ page language="java" contentType="text/html; charset=UTF-8"
  6. pageEncoding="UTF-8"%>
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  8. <html>
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  11. <title>Insert title here</title>
  12. </head>
  13. <body>
  14. <%
  15. // 創(chuàng)建一個map對象,并添加數(shù)據(jù)
  16. HashMap<String,Object> map = new HashMap<String,Object>();
  17. map.put("aaa", "AAAValue");
  18. map.put("bbb", "bbbValue");
  19. map.put("ccc", "cccValue");
  20. map.put("ddd", "dddValue");
  21. // 創(chuàng)建一個list集合對象
  22. List<String> strList = new ArrayList<String>();
  23. strList.add("aaa");
  24. strList.add("bbb");
  25. strList.add("ccc");
  26. // 創(chuàng)建一個User對象
  27. User user = new User("用戶名",new String[]{"第一個電話","第二個電話","第三個電話"},map,strList);
  28. // 把用戶對象添加到請求request的屬性中
  29. request.setAttribute("user", user);
  30. %>
  31. <%--訪問 對象的username 屬性--%>
  32. user對象的username屬性值---->>>>${ user.username }<br/><br/>
  33. <%--訪問user對象中數(shù)組的第二個元素--%>
  34. user對象中phones數(shù)組的第二個元素-------->>>>${ user.phones[1] }<br/><br/>
  35. <%--訪問 list集合 中第一個元素--%>
  36. list集合中第一個元素-------->>>>>${ user.strList[0] }<br/> <br/>
  37. <%--訪問 user對象中map集合的aaa的屬性值 --%>
  38. user對象中map集合中aaa屬性的值----->>>>>${ user.map.aaa }<br/><br/>
  39. </body>
  40. </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運算測試代碼:

  1. <%@page import="java.util.Map"%>
  2. <%@page import="java.util.List"%>
  3. <%@page import="java.util.HashMap"%>
  4. <%@page import="java.util.ArrayList"%>
  5. <%@ page language="java" contentType="text/html; charset=UTF-8"
  6. pageEncoding="UTF-8"%>
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  8. <html>
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  11. <title>Insert title here</title>
  12. </head>
  13. <body>
  14. <%
  15. // 對象為null時,返回true
  16. request.setAttribute("nullObject", null);
  17. //如果是空的字符串,返回true
  18. request.setAttribute("emptyStr", "");
  19. //如果是空的數(shù)組,返回true
  20. request.setAttribute("emptyArr", new Object[]{});
  21. //空的集合,返回true
  22. List list = new ArrayList();
  23. request.setAttribute("emptyList", list);
  24. //空的map,返回true
  25. Map map = new HashMap();
  26. request.setAttribute("emptyMap", map);
  27. %>
  28. <h1>EL empty 運算</h1>
  29. 對象為null,empty為真 ---->>>>${ empty nullObject }<br/>
  30. 空的字符串,empty為真------>>>>${ empty emptyStr }<br/>
  31. 空的數(shù)組,empty為真------>>>>${ empty emptyArr }<br/>
  32. 空的list集合,empty為真---->>>>${ empty emptyList }<br/>
  33. 空的map集合,empty為真----->>>>${ empty emptyMap }<br/>
  34. </body>
  35. </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中含有 “.” , “+” , “_” , “*” , “/” 等的運算字符

示例:

  1. <body>
  2. <%
  3. //設(shè)置
  4. Map map = new HashMap();
  5. map.put("a-b-c","a-b-c-Value");
  6. map.put("a.b.c", "a.b.c.Value");
  7. map.put("aaa","aaa-Value");
  8. request.setAttribute("map", map);
  9. %>
  10. <%-- 下面我們可以通過中括號方式獲取對應(yīng)key的值 --%>
  11. ${ map['a.b.c'] }<br/>
  12. ${ map['a-b-c'] }<br/>
  13. ${ map.aaa }<br/>
  14. </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ù)

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <%
  11. // 在四個域中存放數(shù)據(jù),進行獲取
  12. pageContext.setAttribute("key", "pageContext-value");
  13. request.setAttribute("key", "request-Value");
  14. session.setAttribute("key", "session-value");
  15. application.setAttribute("key", "application-value");
  16. %>
  17. <%-- 從不同的域中獲取數(shù)據(jù) --%>
  18. page域中key的值:${ pageScope.key }<br/>
  19. request域中key的值:${ requestScope.key }<br/>
  20. session域中key的值:${ sessionScope.key }<br/>
  21. application域中key的值:${ applicationScope.key }<br/>
  22. </body>
  23. </html>

   

運行的結(jié)果:

 


pageContext訪問Jsp中內(nèi)置對象(用的不多)。
通過pageContext對象。我們可以直接獲取jsp中的一些內(nèi)置對象,
比如:
request對象,
session對象,
Servletconfig對象,
ServletContext對象,
然后獲取一些我們需要的信息。
常用的功能獲取
協(xié)議:
服務(wù)器ip:
服務(wù)器端口:
獲取工程路徑:
獲取請求方法:
獲取客戶端ip地址:
獲取會話的id編號:

pageContext使用示例代碼

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="pragma" content="no-cache" />
  7. <meta http-equiv="cache-control" content="no-cache" />
  8. <meta http-equiv="Expires" content="0" />
  9. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  10. <title>Insert title here</title>
  11. </head>
  12. <body>
  13. 協(xié)議:${ pageContext.request.scheme }<br/>
  14. 服務(wù)器ip:${ pageContext.request.serverName }<br/>
  15. 服務(wù)器端口:${ pageContext.request.serverPort }<br/>
  16. 獲取工程路徑:${ pageContext.request.contextPath }<br/>
  17. 獲取請求方法:${ pageContext.request.method }<br/>
  18. 獲取客戶端ip地址:${ pageContext.request.remoteHost }<br/>
  19. 獲取會話的id編號:${ pageContext.session.id }<br/>
  20. </body>
  21. </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)容:

  1. <context-param>
  2. <param-name>username</param-name>
  3. <param-value>root</param-value>
  4. </context-param>

  

使用的示例代碼:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. 參數(shù)username的值:${ param.username }<br/>
  11. 參數(shù)hobby的值:${ paramValues.hobby[0] }<br/>
  12. 請求頭Accept-Language的值:${ header["Accept-Language"] }<br/>
  13. 請求頭Accept的值:${ headerValues["Accept"][0] }<br/>
  14. cookie的key = ${ cookie.JSESSIONID.name } : value = ${ cookie.JSESSIONID.value } <br/>
  15. 上下文參數(shù):${ initParam.username }<br/>
  16. </body>
  17. </html>

 

訪問的顯示結(jié)果: