目錄
- JSTL 標簽庫
- JSTL 標簽庫的使用步驟
- core 核心庫使用
- <c:set /> (使用很少)
- <c:if />
- <c:choose><c:when><c:otherwise>標簽
- <c:forEach />
JSTL 標簽庫
JSTL 標簽庫 全稱是指 JSP Standard Tag Library JSP 標準標簽庫,是一個不斷完善的開放源代碼的 JSP 標
簽庫,
EL 運算式主要是為了替換 jsp 中的運算式腳本,而標簽庫則是為了替換代碼腳本,這樣使得整個 jsp 頁面
變得更佳簡潔,
JSTL 由五個不同功能的標簽庫組成,
| 功能范圍 | URI | 前綴 |
|---|---|---|
| 核心 標簽庫-- 重點 | http://java.sun.com/jsp/jstl/core | c |
| 格式化 | http://java.sun.com/jsp/jstl/fmt | fmt |
| 函式 | http://java.sun.com/jsp/jstl/functions | fn |
| 資料庫(不使用) | http://java.sun.com/jsp/jstl/sql | sql |
| XML(不使用) | http://java.sun.com/jsp/jstl/xml | x |
在 jsp 標簽庫中使用 taglib 指令引入標簽庫
CORE 標簽庫
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
XML 標簽庫
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
FMT 標簽庫
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
SQL 標簽庫
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
FUNCTIONS 標簽庫
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
JSTL 標簽庫的使用步驟
1、先匯入 jstl 標簽庫的 jar 包,
-
taglibs-standard-impl-1.2.5.jar
-
taglibs-standard-spec-1.2.5.jar
下載地址https://tomcat.apache.org/download-taglibs.cgi
2、第二步,使用 taglib 指令引入標簽庫,
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
core 核心庫使用
<c:set /> (使用很少)
作用:set 標簽可以往域中保存資料
<body>
<%--
i.<c:set />
作用: set 標簽可以往域中保存資料
域物件 .setAttribute(key,value);
scope 屬性設定保存到哪個域
page 表示 PageContext 域(默認值)
request 表示 Request 域
session 表示 Session 域
application 表示 ServletContext 域
var 屬性設定 key 是多少
value 屬性設定值
--%>
保存之前:${ sessionScope.abc } <br>
<c:set scope="session" var="abc" value="https://www.cnblogs.com/kohler21/p/abcValue"></c:set>
<%-- <c:set scope="session" var="abc" value="https://www.cnblogs.com/kohler21/p/abcValue"/>--%>
保存之后:${ sessionScope.abc } <br>
</body>
結果:

<c:if />
if 標簽用來做 if 判斷,
例:
<body>
<%--
ii.<c:if />
if 標簽用來做 if 判斷,
test 屬性表示判斷的條件(使用 EL 運算式輸出)
--%>
<c:if test="${ 12 == 12 }">
<h1>12 等于 12</h1>
</c:if>
<c:if test="${ 12 != 12 }">
<h1>12 不等于 12</h1>
</c:if>
</body>
結果:

<c:choose><c:when><c:otherwise>標簽
作用:多路判斷,跟 switch ... case .... default 非常接近
<body>
<%--
<c:choose> <c:when> <c:otherwise> 標簽
作用:多路判斷,跟 switch ... case .... default 非常接近
choose 標簽開始選擇判斷
when 標簽表示每一種判斷情況
test 屬性表示當前這種判斷情況的值
otherwise 標簽表示剩下的情況
<c:choose> <c:when> <c:otherwise> 標簽使用時需要注意的點:
1 、標簽里不能使用 html 注釋,要使用 jsp 注釋
2 、 when 標簽的父標簽一定要是 choose 標簽
--%>
<%
request.setAttribute("score", 92);
%>
<c:choose>
<%-- 這是 html 注釋 --%>
<c:when test="${ requestScope.score > 90 }">
<h2>優秀</h2>
</c:when>
<c:when test="${ requestScope.score > 80 }">
<h2>良好</h2>
</c:when>
<c:when test="${ requestScope.score > 60 }">
<h2>及格</h2>
</c:when>
<c:otherwise>
<c:choose>
<c:when test="${requestScope.score > 60}">
<h3>大于 60</h3>
</c:when>
<c:when test="${requestScope.score > 50}">
<h3>大于 50</h3>
</c:when>
<c:when test="${requestScope.score > 40}">
<h3>大于 40</h3>
</c:when>
<c:otherwise>
其他小于 40
</c:otherwise>
</c:choose>
</c:otherwise>
</c:choose>
</body>
結果:

<c:forEach />
作用:遍歷輸出使用,
-
遍歷 1 到 10 ,輸出
示例代碼:<body> <%--1. 遍歷 1 到 10 ,輸出 begin 屬性設定開始的索引 end 屬性設定結束的索引 var 屬性表示回圈的變數 ( 也是當前正在遍歷到的資料 ) for (int i = 1; i < 10; i++) --%> <table border="1"> <c:forEach begin="1" end="10" var="i"> <tr> <td>第${i}行</td> </tr> </c:forEach> </table> </body>
-
遍歷 Object 陣列
示例代碼:<body> <%-- 2. 遍歷 Object 陣列 for (Object item: arr) items 表示遍歷的資料源(遍歷的集合) var 表示當前遍歷到的資料 --%> <% request.setAttribute("arr", new String[]{"18736635442","18688886666","18699998888"}); %> <c:forEach items="${ requestScope.arr }" var="item"> ${ item } <br> </c:forEach> </body>

-
遍歷 Map 集合
示例代碼:
<body> <% Map<String,Object> map = new HashMap<String, Object>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); // for ( Map.Entry<String,Object> entry : map.entrySet()) { // } request.setAttribute("map", map); %> <c:forEach items="${ requestScope.map }" var="entry"> <h5>${entry.key} = ${entry.value}</h5> </c:forEach> </body>結果:

-
遍歷 List 集合---list 放 中存放 Student 類 , 有屬性 : 編號 , 用戶名 , 密碼 , 年齡 ,
電話資訊
Student 類:public class Student { //4. 編號,用戶名,密碼,年齡,電話資訊 private Integer id; private String username; private String password; private Integer age; private String phone; }jsp:
<body> <%--4. 遍歷 List 集合 ---list 中存放 Student 類,有屬性:編號,用戶名,密碼,年齡,電話資訊 --%> <% List<Student> studentList = new ArrayList<Student>(); for (int i = 1; i <= 10; i++) { studentList.add(new Student(i,"username"+i ,"pass"+i,18+i,"phone"+i)); } request.setAttribute("stus", studentList); %> <table> <tr> <th>編號</th> <th>用戶名</th> <th>密碼</th> <th>年齡</th> <th>電話</th> <th>操作</th> </tr> <%-- items 表示遍歷的集合 var 表示遍歷到的資料 begin 表示遍歷的開始索引值 end 表示結束的索引值 step 屬性表示遍歷的步長值 varStatus 屬性表示當前遍歷到的資料的狀態 for ( int i = 1; i < 10; i+=2 ) --%> <c:forEach begin="1" end="10" step="2" varStatus="status" items="${requestScope.stus}" var="stu"> <tr> <td>${stu.id}</td> <td>${stu.username}</td> <td>${stu.password}</td> <td>${stu.age}</td> <td>${stu.phone}</td> <td>添加、洗掉</td> </tr> </c:forEach> </table> </body>
歡迎關注公眾號:愚生淺末

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/502964.html
標籤:Java
上一篇:JSP中的EL 運算式
下一篇:行程、執行緒補充與協程相關介紹
