9、EL和JSTL介紹及使用

什麼是EL

Expression Language表達式語言,EL和JSTL一起使用取代JSP頁面中嵌入java代碼寫法。

  • EL功能

    • 獲取數據
    • 執行運算
    • 獲取web開發常用對象
  • EL寫法
    ${ };

EL——獲取數據

EL表達式語句在執行的時候,會調用pageContext.findAttribute()方法。分別從page、request、session、application範圍查找相應對象,找到就會返回相應對象,找不到返回“”(不是null,是空的字符串),EL獲取數據需要在四個作用範圍中。

<% pageContext.setAttribute("name","張三");
	request.setAttribte("name","李四");
	session.setAttribte("name","王五");
	application.setAttribte("name","趙六");
%>
${pageScope.name};
${requestScope.name};
${sessionScope.name};
${applicationScope.name};
<h3>EL的簡寫</h3>
${ name }    <!-- 底層調用pageContext.findAttribute()-->

EL獲取數組和集合數據:

<h3>EL獲取數組數據</h3>
<% String[] arrs = {"aa","bb","cc"};
	pageContext.setAttribute("arrs",arrs);
%>
${ arrrs[0] }
${ arrrs[1] }
${ arrrs[2] }

<h3>EL獲取List集合數據</h3>
<% List<String> list = new ArrayList<String>();
	list.add("aa");
	list.add("bb");
	list.add("cc");
	pageContext.setAttribute("list",list);
%>
${ list[0] }
${ list[1] }
${ list[2] }

<h3>EL獲取Map集合數據</h3>
<% Map<String,Integer> map = new HashMap<String,Integer>();
	map.put("111",aaa);
	map.put("222",bbb);
	map.put("333.444",ccc);
	pageContext.setAttribute("map ",map);
%>
${ map.111 }
${ map.222 }
${ map["333.444"] }

EL——執行運算

<%pageContext.setAttribute("n1","10");
	pageContext.setAttribute("n1","10");
%>
<h3>執行算數運算</h3>
${ n1+n2 }
<h3>執行關係運算,返回true or false</h3>
${ n1 < n2} -- ${n1 lt n2} <br />
${ n1 > n2} -- ${n1 gt n2} <br />
${ n1 == n2} -- ${n1 eq n2} <br />
${ n1 >= n2} -- ${n1 ge n2} <br />
${ n1 <= n2} -- ${n1 le n2} <br />
${ n1 != n2} -- ${n1 ne n2} <br />

<%pageContext.setAttribute("n3","30");
	pageContext.setAttribute("n4","40");
%>
<h3>執行邏輯運算</h3>
${(n1 < n2) && (n3 < n4)} -- ${(n1 < n2) and (n3 < n4)}<br/>
${(n1 < n2) || (n3 < n4)} -- ${(n1 < n2) or (n3 < n4)}<br/>
${!(n1 < n2)} -- ${not(n1 < n2)}<br/>
<h3>執行三元運算</h3>
${n1 < n2? "n1小於n2":"n1不小於n2"}
<h3>空運算符</h3>
${empty n1}

EL——獲取web開發常用對象

EL表達式定義了11個web開發常用對象。使用這些對象可以很方便的獲取web開發中的一些常見對象,並可以讀取這些對象中的數據。

在這裏插入圖片描述
在這裏插入圖片描述

什麼是JSTL

JSP StandardTag Library,JSP標準標籤庫。

  • 主要和EL一起取代傳統頁面上直接嵌入java代碼的寫法,提升程序可讀性、維護性和方便性。
  • 使用需要引入jstl.jar以及standard.jar兩個jar包。
  • JSP頁面引入標籤庫(固定寫法,C:核心標籤庫)
    在這裏插入圖片描述
<% pageContext.setAttribute("name","aaa");
%>
<c:set value="aaa" var="name" scope="page"></c:set>
${ name }

JSTL——if標籤

  • if標籤的使用
<c:set value="10" var="i" scope="page"></c:set>
<c:if test="${ i >= 10}">
	<font color="red">i大於等於10</font>
</c:if>
<c:if var="flag" test="${ i < 10}" scope="page">
	<font color="blue">i小於10</font>
</c:if>
<c:if test="${ flag }">
	flag爲true
</c:if>
  • if標籤的屬性

    • test屬性:條件
    • var屬性:將test中的條件的值賦給一個變量,在var中定義變量
    • scope屬性:作用範圍

JSTL——foreach標籤

  • foreach標籤的使用
<h3>遍歷數組</h3>
<% String[] arrs = {"aa","bb","cc"};
	pageContext.setAttribute("arrs",arrs);
%>
<c:forEach var="s" items="${ arrs }">
	${ s }
</c:forEach>

<h3>遍歷List集合</h3>
<% List<String> list = new ArrayList<String>();
	list.add("aa");
	list.add("bb");
	list.add("cc");
	pageContext.setAttribute("list",list);
%>
<c:forEach var="s" items="${ list }">
	${ s }
</c:forEach>

<h3>遍歷Map集合</h3>
<% Map<String,Integer> map = new HashMap<String,Integer>();
	map.put("111",aaa);
	map.put("222",bbb);
	map.put("333",ccc);
	pageContext.setAttribute("map ",map);
%>
<c:forEach var="entry" items="${ map }">
	${ entry.key } -- ${ entry.value }
</c:forEach>

<h3>遍歷從110</h3>
<c:forEach var="i" begin="1" end="10" step="1">
	${ i }
</c:forEach>

<h3>遍歷100200,每次加2,到第三個數的時候將該數字變藍</h3>
<c:forEach var="i" begin="100" end="200" step="2" varStatus="status">
	<c:if test="${ status.count % 3 == 0 }">
		<font color="blue">${ i }</font>
	</c:if>
	<c:if test="${ status.count % 3 != 0 }">
		${ i }
	</c:if>
</c:forEach>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章