EL表達式
一、什麼是EL:
1.1、EL
– Expression Language 表達式語言
1.2、文法:
${EL表達式}
1.3、與JSP表達式差別:
JSP中使用的變量是腳本變量,而EL中的變量都是作用域變量,要操作的變量都在作用域中。
二、EL表達式的作用
2.1、用于表達式的運算。如:加、減、乘、除。
2.2、用于從作用域中取出資料
三、擷取資料
3.1、在四個域中查找:比如在一個域中設定了“color”屬性,現在擷取
EL: ${color},功能與pageContext.findAttribute(“color”) 一樣。
3.2、指定域擷取資料
作用域 | Java代碼 | EL的寫法 |
---|---|---|
頁面域 | pageContext.getAttribute(“color”); | ${pageScope.color} |
請求域 | request.getAttribute(“color”); | ${requestScope.color} |
會話域 | session.getAttribute(“color”); | ${sessionScope.color} |
上下文域 | application.getAttribute(“color”); | ${applicationScope.color} |
自動查找 | pageContext.findAttribute(“color”); | ${color} |
3.3、EL中11個隐式對象
隐含對象名稱 | 描述 |
---|---|
pageContext | 代表頁面上下文對象,可以在頁面上調用get方法 |
pageScope | 代表頁面域中的Map對象 |
requestScope | 代表請求域中的Map對象 |
sessionScope | 代表會話域中的Map對象 |
applicationScopeScope | 代表上下文域中的Map對象 |
param | 得到表單送出參數,功能與:request.getParameter()相同 |
paramValues | 得到表單送出參數,功能與:String[] request.getParameterValues()相同 |
header | 得到請求頭的資料 request.getHeader(“名字”) |
headerValues | 得到請求頭的資料 request.getHeaders(“名字”) |
cookie | 得到請求的Cookie資訊 |
initParam | 相當于config.getInitParamter()得到web.xml中配置的參數 |
3.4、pageContext調用get的方法
作用 | JSP表達式 | EL的寫法 |
---|---|---|
目前工程路徑 | <%=request.getContextPath() %> | ${pageContext.request.contextPath} |
請求資源路徑 | <%=request.getContextURL() %> | ${pageContext.request.contextURL} |
通路者的IP | <%=request.getRemoteAddr() %> | ${pageContext.request.remoteAddr} |
目前會話的ID | <%=request.getSession().getId() %> | ${pageContext.request.session.id} |
3.5、得到Cookie中的值:
先通過EL得到指定cookie的對象,再通過name和value得到具體的名字和值
<%
Cookie c = new Cookie(“man”,”jack”);
response.addCookie(c);
%>
讀取Cookie:
名字: cookie.man.name<值: {cookie.man.value } <%– 調用 cookie.getValue() –%>
3.6、EL表達式中[]和.的差別
如果一個變量名中有特殊的字元,則應用使用[“名字”];
比如擷取header中host屬性,一共有兩種寫法:
header[“host”] {header.host }
若擷取user-agent屬性,隻能使用中括号
${header[“user-agent”]}
四、EL表達式擷取不同類型的資料
4.1擷取JavaBean的屬性值
<%
//建立一個對象
Student stu = new Student(“張三”, 20);
stu.setMale(true);
//把對象放到域
pageContext.setAttribute(“a”, stu);
%>
姓名:${a.name} 年齡:${a.age} 性别:${a.male}
4.2擷取數組中的值
<%
String[] arr = {“aaa”,”bbb”,”cccc”};
pageContext.setAttribute(“arr”, arr);
%>
取數組中的元素:
${arr[0]}
${arr[1]}
${arr[2]}
4.3擷取Map中的值
<%
map.put(“n1”, “豬八戒”);
map.put(“n2”, “唐僧”);
map.put(“n3”, “孫悟空”);
//放在請求域中
request.setAttribute(“map”, map);
%>
${map.n1}
${map[“n2”]}
${map.n3}
4.4輸出List中指定索引位置的元素
<%
book.add(“水浒傳”);
book.add(“西遊記”);
book.add(“紅樓夢”);
session.setAttribute(“books”, book);
%>
${books[0] }
${books[1] }
${books[2] }
五、EL中使用表達式
5.1算術表達式 :
算術運算符 | 說明 | 範例 | 結果 |
---|---|---|---|
+ | 加 | ${1+1} | 2 |
- | 減 | ${2-1} | 1 |
* | 乘 | ${1*1} | 1 |
/或div | 除 | ${5 div 2} | 2.5 |
%或mod | 取餘 | ${5 mod 2} | 1 |
5.2比較表達式
關系運算符 | 說明 | 範例 | 結果 |
---|---|---|---|
==或 eq | 等于(equal) | ${1 eq 1} | true |
!= 或 ne | 不等于(not equal) | ${1 != 1} | false |
< 或 lt | 小于(Less than) | ${1 lt 2} | true |
<=或 le | 小于等于(Less than or equal) | ${1 <= 1} | true |
> 或 gt | 大于(Greater than) | ${1 > 2} | false |
>=或 ge | 大于等于(Greater than or equal) | ${1 >=1} | true |
5.3邏輯表達式 :
邏輯運算符 | 說明 | 範例 | 結果 |
---|---|---|---|
&& 或 and | 交集(與) | ${A and B} | true / false |
|| 或 or | 并集(或) | ${A || B } | true / false |
! 或 not | 非 | ${not A} | true / false |
5.4三元運算:
${表達式?真:假}
5.5判空表達式:
5.5.1文法:
${empty 變量名} 判斷變量名是否為空或空串,如果是,則傳回true
5.5.2作用:
1)判斷一個變量是否為null
2)判斷一個字元串是否是空串
3)判斷一個集合是否有元素
<%
request.setAttribute(“num”, 5);
request.setAttribute(“str”, “”);
//建立一個集合
ArrayList list = new ArrayList();
pageContext.setAttribute(“list”, list);
%>
1) 建立一個變量是否為null
${empty num}
2) 建立一個字元串是否是空串
${empty str}
3) 建立一個集合是否有元素
${empty list }
5.6EL表達式的啟用和禁用
5.6.1頁面禁用
isELIgnored=”true” 忽略EL表達式,EL不起作用。
5.6.2 EL的轉義
\ ${5 div 3 } \表示這個表達式不起作用