EL表達式學習
1.什么是EL表達式
全稱:Expression Language.一種寫法非常簡潔的表達式。語法簡單易懂,便於使用。表達式語言的靈感來自於ECMAScript和XPath表達式語言。
2.EL表達式的作用
作用:讓jsp書寫起來更加的方便。簡化在jsp中獲取作用域或者請求數據的寫法。也會搭配Jstl來進行使用。
3.使用EL表達式
語法結構:${expression}:提供 . 和 [] 兩種運算符來存取數據。
使用:
a)使用EL表達式獲取請求數據
- 獲取用戶請求數據(請求實體)
${param.鍵名}獲取請求實體中一個鍵一個值的數據
${paramValues.鍵名}獲取請求實體中同鍵不同值的數據,返回的是String數組,可以使用角標直接獲取 例如:愛好
- 獲取請求頭數據
${header}返回所欲的請求數據,鍵值對形式
${header['鍵名']}返回指定的鍵的請求頭數據
${headerValues[‘鍵名’]}
- 獲取cookie數據
${cookie}獲取所有cookie對象 鍵值對
${cookie.Cookie對象的鍵名} 獲取了指定cookie數據的cookie對象
${cookie.Cookie對象的鍵名.name}獲取存儲了指定cookie數據的cookie對象的存儲的鍵
${cookie.Cookie對象的域名.value}獲取存儲了指定cookie數據的cookie對象存儲的值
b)使用EL表達式獲取作用域數據
作用域對象:pageContext request session application
獲取作用域數據:我們使用setAttribute方法存儲的數據
獲取:
普通字符串數據 ${鍵名}
對象數據 ${鍵名.屬性名}
集合數據
list集合 ${鍵名[角標]}
Map集合 ${map集合作用域鍵名。map集合存儲的數據的鍵名}
作用域查找順序: 如果找到了則不再查找,如果找不到,則繼續查找下一個作用域,如果四個作用域中都沒有則什么都不顯示。
pageContext---->request---->session--->application
指定作用域取數據:
${pageScope.鍵名} 指明獲取pagecontext作用域中的數據
${requestScope.鍵名}指明獲取request作用域中的數據
${sessionScope.鍵名}指明獲取session作用域中的數據
${applicationScope.鍵名}指明獲取application作用域中的數據
${cookie.user.username}
c)使用EL表達式運算
算術運算:+ - * / 在EL表達式中可以直接進行算術運算
關系運算: && || !
${邏輯表達式}
注意:沒有單&單|
比較運算:== != >, >=, <,<=
${比較表達式}
三目運算符:
${條件?值:值}
d)EL表達式空值判斷empty
${empty 鍵名}
作用:
判斷該鍵是否存儲有效數據。

<%@ page language="java" import="java.util.*,wq.pojo.*" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!-- 將流轉數據顯示到瀏覽器中,使用傳統的方式 --> <%=request.getAttribute("str")%><br> <%=((User)request.getAttribute("user")).getFav()%><br> <%=((ArrayList)request.getAttribute("list")).get(2)%><br> <%=((User)((ArrayList)request.getAttribute("list")).get(3)).getFav()%><br> <%=((HashMap)request.getAttribute("hs")).get("b1")%><br> <%=((User)((HashMap)request.getAttribute("hs")).get("c1")).getFav()%> <hr> ${str}<br> ${user.fav}<br> ${list[2]}<br> ${list[3].fav}<br> ${hs.b1}<br> ${hs.u.fav}<br> <hr> <h3>獲取請求實體數據(請求實體)</h3> <%=request.getParameter("uname")%>---${param.uname}<br> <%=request.getParameterValues("fav")[1]%>--${paramValues.fav[1]}<br> <%=request.getHeader("User-Agent")%>---${header["User-Agent"]}--${headerValues["Accept-Language"][0]}<br> <h3>獲取cookie數據</h3> ${cookie}<br> ${cookie.name}<br> ${cookie.JSESSINI.name}--${cookie.JSESSIONID.value} <h3>EL獲取作用域數據時作用域的查找順序</h3> <% pageContext.setAttribute("hello", "hello pageContext"); request.setAttribute("hello","hello rquest"); session.setAttribute("hello", "hello session"); application.setAttribute("hello", "hello application"); %> ${requestScope.hello}--${a} <h3>EL表達式的邏輯運算</h3> <% request.setAttribute("a", "2"); %> ${1+2}---${2*3}--${a+3}---${1+"5"} ${2>3&&4<5}---&{2>3||4<5}--${!(2>3)} ${2==2}---${3>2}<br> ${2>3?"男":"女"} <h3>EL表達式的empty</h3> <% request.setAttribute("str", ""); User u=new User(); request.setAttribute("name", u); ArrayList la=new ArrayList(); request.setAttribute("la", la); %> ${empty str}<br> ${u}<br> ${la}<br>
JSTL標簽庫
1.什么是jstl標簽庫
- jstl是apache對EL表達式的擴展(也就是jstl依賴EL),JSTL是標簽語言!JSTL標簽使用以來非常方便,它與jsp動作標簽一樣,只不過他不是jsp內置標簽,需要我們自己導包,以及指定標簽而已。
- 如果你使用MyEclipse開發javaWeb,那么在把項目發布到tomcat時,你會發現,myeclipse會在lib目錄下存放jstl的jar包!如果你沒有使用myeclipse開發那么需要自己來導入這個jstl的jar包:jstl-1.2.jar
2.JSTL標簽庫的作用
- 用來提升在jsp頁面的邏輯代碼的編碼效率,使用標簽來代替邏輯代碼的直接書寫,高效,美觀,整潔,易讀。
3.使用JSTL標簽庫
a)內容:
- i.核心標簽庫(學習)(學習)
- ii.格式化標簽庫
- iii.函數標簽庫
- iv.XML標簽庫
- v.SQL標簽庫
b)使用:
i.導包
ii.使用taglib標簽引入資源
iii.核心標簽庫。
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
內容:
out標簽:<c:out value="${表達式}" default="默認值" ></c:out>
作用:結合EL表達式將數據響應給瀏覽器。如果EL表達式沒有取到數據則可以使用default屬性聲明默認值。
set標簽: <c:set value="數據" var="鍵名" scope="作用域名"></c:set>
作用:將數據存儲到指定的作用域中,默認值pageContext作用域
remove標簽:<c:remove var="要刪除數據的鍵名" scope="作用域名"/>
作用:刪除作用域中的數據,默認是刪除四個作用域中符合要求的數據。
但是可以通過scope屬性指明要刪除的作用域數據
注意:
使用pageContext.removeAttribute("鍵名"); 此方法將四個作用域中符合的數據都會刪除
使用pageContext.removeAttribute(String name,int scope);
要刪除的作用域中的數據scope的值為1.pageContext,2.request,3.session,4.application
使用request.removeAttribute("鍵名"); 刪除當前作用域中符合要求的數據
使用session.removeAttribute("鍵名"); 刪除當前作用域中符合要求的數據
使用application.removeAttribute("鍵名");刪除當前作用域中符合要求的數據
邏輯標簽:
單分支判斷標簽:<c:If test="${表達式}">數據</c:if>
作用:可以根據el表達式進行一定程度的單分支邏輯判斷。
注意:
test屬性中書寫的是EL表達式,或者說是EL表達式。
該標簽只能進行EL表達式相關的邏輯判斷,不能進行EL表達式不能獲取的數據的邏輯處理。
多分支判斷標簽:
<c:hoose>
<c:when test="${表達式}"><c:when>
<c:when test=" ${表達式}"><c:when>
..
<c:otherwise></c:otherwise>
注意:
符合條件后執行一個分支,其他分支不會執行。
循環標簽:
<c:foreach>
循環體
</c:foreach>
屬性:
begin:聲明循環的開始位置
end:聲明循環的結束位置
step:聲明循環的步長
varStatus:聲明變量記錄循環狀態,例如變量:i
注意:記錄的數據存儲到作用域中可以直接使用EL表達式進行獲取
實例:${i.index} 獲取當次循環的下標
${i.count} 獲取當次循環的次數
${i.first} 判斷是否第一次循環
${i.last}判斷是否是最后一次循環
items:聲明要遍歷的數據,可以是集合和數組等
注意:要使用EL表達式來獲取
var:聲明變量記錄每次遍歷的結果。可以做循環體中使用EL表達式獲取遍歷出來的數據。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% request.setAttribute("str", "jstl"); %> <%request.setAttribute("str1","jstl學習");%> -----------${str1} --------------<c:out value="${str2}" default="404"></c:out> <hr> <!-- set標簽學習 --> <% request.setAttribute("s1", "set標簽學習"); %> <c:set value="set標簽學習2" var="s2" scope="request"></c:set> <c:set value="hello pageContext" var="hello" scope="page"></c:set> <c:set value="hello request" var="hello" scope="request"></c:set> <c:set value="hello session" var="hello" scope="session"></c:set> <c:set value="hello application" var="hello" scope="application"></c:set> ${s1}---${requestScope.s2}---${pageScope.hello} <br> ${hello} <hr> <!-- remove標簽學習 --> <%-- <c:out value="${hello}"></c:out> --%> <% pageContext.removeAttribute("hello"); request.removeAttribute("hello"); %> ${pageScope.hello} ${hello} <hr> <!-- 單分支判斷標簽 --> <c:set var="a" value="12"></c:set> <% int a=Integer.parseInt((String)pageContext.getAttribute("a")); if(a>8){ %> <b>今天天氣真好,適合學習1</b> <% } %> <c:if test="${a>8}"> <b>今天天氣真好,適合學習2</b> </c:if> <hr> <!-- 多分枝邏輯判斷 --> <c:set var="scope" value="100"></c:set> <c:choose> <c:when test="${scope>=90}"> <i>蘋果</i> </c:when> <c:when test="${scope>=70&&score<80}"> <i>蘋果</i> </c:when> <c:when test="${scope>=80&&scope<70}"></c:when> <c:otherwise> <i>男女混合雙打</i> </c:otherwise> </c:choose> <br> <hr> <!-- 循環標簽 --> <c:forEach begin="0" end="5" step="1" varStatus="i"> <c:if test="${i.count==3}"> <u>我是第三次循環</u> </c:if> 111111-${i.index}--${i.count}---${i.first}---${i.last}<br> </c:forEach> <!-- 循環標簽遍歷集合 --> <% //創建測試數據list ArrayList<String> list=new ArrayList<String>(); list.add("蘋果"); list.add("榴蓮"); list.add("荔枝"); request.setAttribute("list", list); %> <c:forEach items="${list}" var="s" varStatus="i"> ${s}--${i.index}---${i.count}<br> </c:forEach> <!-- 遍歷map集合 --> <% //聲明map集合測試數據 HashMap<String,String> hs=new HashMap<String,String>(); hs.put("s1", "唱歌"); hs.put("s2", "跳舞"); hs.put("s3", "敲代碼"); //將數據存儲到作用域中 request.setAttribute("hs", hs); %> <c:forEach items="${hs}" var="s"> ${s.key}---${s.value}<br> </c:forEach>