EL和JSTL
1、EL的使用
EL:是一種表達式語言,它可以直接在jsp界面用,可以減少很多代碼。使用EL之前取域中的數據是<%= >
-
EL寫法:${key};
-
EL數據域範圍(page->request->session->application),先取前面的
-
如果el表達式不帶取值範圍時,依次從page->request->session->application尋找Key所對應的value,如果從page範圍內找到值,就不會再從後面的範圍尋找.
-
<!-- 在指定範圍內存值 -->
-
pageContext域:${pageScope.key};
-
request域:${requestScope.key};
-
session域:${sessionScope.key};
-
application域:${applicationScope.key};
-
-
如果${Key}的Key不存在或者通過key得到null或""的值就不顯示不報錯不佔頁面位置,不會報null.
-
如果${Key}的Key不存在或者通過key得到null或""的值就不顯示不報錯不佔頁面位置,不會報null.
-
判斷el表達式中值是否是null或""用: ${empty key}或${!empty key}
-
el表達式有自動類型轉換作用
<% //將數據存在page範圍內 pageContext.setAttribute("sname", "吳鵬程"); //同一個範圍內Key名相同,value覆蓋 pageContext.setAttribute("sname", "唐志亮"); //將數據存在request範圍內 request.setAttribute("sname", 18); //將數據存在session範圍內 session.setAttribute("sname", "man"); //將數據存在application範圍內 application.setAttribute("sname", "愛好女"); //將對象存在request範圍內 Student student1=new Student("悌子君", "123456", "man", "[email protected]", "rap", "西安"); request.setAttribute("stu1", student1); //將list集合存在request範圍內 //創建集合 List<Student> stuList=new ArrayList(); //向集合中添加元素 stuList.add(new Student("aa", "123456", "man", "[email protected]", "rap", "西安")); stuList.add(new Student("bb", "123456", "man", "[email protected]", "rap", "西安")); stuList.add(new Student("cc", "123456", "man", "[email protected]", "rap", "西安")); request.setAttribute("stuList1", stuList); /* 在java的泛型集合中這三種聲明都可以 Map<String,String> map1=new HashMap(); Map<String,String> map1=new HashMap<>();這種創建集合的方式,注意有的版本el表達式識別不了 Map<String,String> map1=new HashMap<String,string>(); */ //將map集合存在request範圍內 //創建集合 Map<String,String> map1=new HashMap(); //向集合中添加元素 map1.put("k1", "楊森林"); map1.put("k2", "段怡豪"); request.setAttribute("map2", map1); //將map集合的key存數字類型存在request範圍內 Map<Long,String> map4=new HashMap(); //向集合中添加元素 map4.put(2l, "張俊飛"); request.setAttribute("map3", map4); %> <!-- 用el表達式取出page範圍內的值 --> <h1>${sname}</h1> <!-- 用el表達式取出request範圍內的值 --> <h1>${sage}</h1> <!-- 用el表達式取出session範圍內的值 --> <h1>${ssex}</h1> <!-- 用el表達式取出application範圍內的值 --> <h1>${shobby}</h1> <!-- 在指定範圍內存值 --> <h2>${pageScope.sname }</h2> <h2>${requestScope.sname }</h2> <h2>${sessionScope.sname }</h2> <h2>${applicationScope.sname }</h2> <!--用el取出對象的屬性值 --> <h3>姓名:${stu1.name },性別:${stu1.sex }</h3> <h3>${stu1.toString() }</h3> <!-- el取出對象輸出時,會自動調用對象toString() --> <h3>${stu1 }</h3> <!-- 用el表達式取出list集合中元素 --> <h1>${stuList1[0]}</h1> <!-- 用el表達式取出list集合中元素的屬性 --> <h1>${stuList1[0].name}</h1> <!-- 用el表達取出map集合中元素 --> <h1>${map2["k1"] }</h1> <h1>${map2.k1 }</h1> <!-- el中直接用運算符 --> <h1>${1+6-1 }</h1> <h2>${1<2&&3<4 }</h2> <!-- el中判斷key爲不爲空 --> <h1>不爲空的判斷:${!empty kk }</h1> <h1>爲空的判斷${empty kk }</h1> <!-- el表達式中+只有算術運算作用 --> ${"1"+1 } <!-- el表達式只支持map集合的key用Long或String不支持Integer --> <h1>${map3[2] }</h1>
JSTL使用
JSTL(JSP Standard Tag Library),JSP標準標籤庫,可以嵌入在jsp頁面中使用標籤的形式完成業務邏輯等功能。jstl出現的目的同el一樣也是要代替jsp頁面中的腳本代碼。JSTL標準標籤庫有5個子庫,但隨着發展,目前常使用的是他的核心庫。
-
-
下載:Apache的網站下載JSTL的JAR包。進入“http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/”網址下載 JSTL的壓縮包。jakarta-taglibs-standard-1.1.2.zip
-
導入:解壓後在lib目錄下可以看到兩個JAR文件,分別爲jstl.jar和standard.jar。其中,jstl.jar文件包含JSTL規範中定義的接口和相關類,standard.jar文件包含用於實現JSTL的.class文件以及JSTL中5個標籤庫描述符文件(TLD),將兩個文件導入項目中。
-
在jsp頁面導入標籤庫: <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
-
jstl的通用標籤(瞭解):
eg:<!-- 聲明變量並賦值的標籤 --> <c:set var="sname" value="張三" ></c:set> <!-- 輸出標籤 --> <c:out value="${sname }"></c:out> <h1>用el取出的值:${sname }</h1> <!-- 刪除變量的值的標籤 --> <c:remove var="sname"></c:remove> <h2>用刪除標籤後的值爲:${sname }</h2>
-
jstl選擇標籤:
-
if選擇標籤:
<c:if test="條件">代碼</c:if>
<c:if test="${1>2 }"> <h1>1小於2</h1> </c:if>
-
choose選擇標籤:choose相當於java中多重if選擇結構
<c:choose> <c:when test="條件1">代碼塊1</c:when> <c:when test="條件2">代碼塊2</c:when> <c:when test="條件3">代碼塊3</c:when> <c:otherwise>上面所有條件不成立時,就執行的代碼塊</c:otherwise> </c:choose> eg:<!-- choose相當於java中多重if選擇結構 --> <c:set var="score" value="59"></c:set> <h1> <c:choose> <c:when test="${score>=90 }">優秀</c:when> <c:when test="${score>=80 }">良好</c:when> <c:when test="${score>=60 }">及格</c:when> <c:otherwise>再接再厲</c:otherwise> </c:choose> </h1>
c:choose> <c:when test="條件1">代碼塊1</c:when> <c:when test="條件2">代碼塊2</c:when> <c:when test="條件3">代碼塊3</c:when> <c:otherwise>上面所有條件不成立時,就執行的代碼塊</c:otherwise> </c:choose> eg:<!-- choose相當於java中多重if選擇結構 --> <c:set var="score" value="59"></c:set> <h1> <c:choose> <c:when test="${score>=90 }">優秀</c:when> <c:when test="${score>=80 }">良好</c:when> <c:when test="${score>=60 }">及格</c:when> <c:otherwise>再接再厲</c:otherwise> </c:choose> </h1> -
jstl循環標籤:
-
foreach當前for循環來使用:
<c:forEach var="當前遍歷變量名" begin="起始值" end="終止值" step="疊加值"> 代碼 </c:forEach> eg:<!-- for循環使用:從1輸出10 --> <c:forEach var="num" begin="1" end="10" step="2"> ${num } </c:forEach>
c:forEach var="當前遍歷變量名" begin="起始值" end="終止值" step="疊加值"> 代碼 </c:forEach> eg:<!-- for循環使用:從1輸出10 --> <c:forEach var="num" begin="1" end="10" step="2"> ${num } </c:forEach> -
foreach循環使用
<c:forEach var="指代當前遍歷元素" items="集合/數組" varStatus="stutus"> 索引:${stutus.index }-當前遍歷的元素的值:${指代當前遍歷元素 } /c:forEach
eg:<% List<String> names=new ArrayList(); names.add("張三"); names.add("李四"); names.add("王二麻子"); request.setAttribute("stuNames", names); %> <h1> <c:forEach var="stu" items="${stuNames }" varStatus="stutus"> ${stutus.index }-${stu } </c:forEach> </h1>
List<String> names=new ArrayList(); names.add("張三"); names.add("李四"); names.add("王二麻子"); request.setAttribute("stuNames", names); %> <h1> <c:forEach var="stu" items="${stuNames }" varStatus="stutus"> ${stutus.index }-${stu } </c:forEach> </h1>
-
-
-
-
函數標籤庫:<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
eg:<!-- 函數標籤庫的使用:相當java中String類常用方法 --> <h2>${fn:contains("我","千") }</h2> <h2>${fn:length("abdfefe") }</h2> <h2>${"abc" eq "abc"}</h2> <h2>${"abc" == "abc"}</h2>
-
格式化標籤庫:<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
eg:<!-- 格式化標籤庫的使用 -->
<%
Date today=new Date();
request.setAttribute("time1", today);
%>
<!-- 格式化日期 -->
<h1><fmt:formatDate value="${time1 }" pattern="yyyy-MM-dd HH:mm:ss"/></h1>
<!-- 保留兩位小數,四捨五入 -->
<h2><fmt:formatNumber value="3.1465926" pattern="0.00"></fmt:formatNumber></h2>
<!-- 將小數轉換爲百分比 -->
<h2><fmt:formatNumber value="0.4267" pattern="0.00%"></fmt:formatNumber></h2>