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” %>
1.JSTL 标簽庫的使用步驟
1、先導入 jstl 标簽庫的 jar 包。
taglibs-standard-impl-1.2.1.jar
taglibs-standard-spec-1.2.1.jar
2、第二步,使用 taglib 指令引入标簽庫。
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
2.core 核心庫使用
2.1 <c:set />(使用很少)
作用:set 标簽可以往域中儲存資料
<body>
<%--
<c:set />(使用很少)
作用:set 标簽可以往域中儲存資料
域對象.setAttribute(key,value);
scope 屬性設定儲存到哪個域
page 表示 PageContext 域(預設值)
request 表示 Request 域
session 表示 Session 域
application 表示 ServletContext 域
var屬性設定key是多少
value屬性設定值是多少
--%>
儲存之前:${requestScope.abc} <br>
<c:set scope="request" var="abc" value="abcValue"/>
儲存之後:${requestScope.abc} <br>
</body>
效果展示:
2.2 <c:if />
if 标簽用來做 if 判斷。
<%--
<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>
效果展示:
2.3 < c:choose > < c:when > < c:otherwise >标簽
作用:多路判斷。跟 switch … case … default 非常接近
<%--
<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("height", 180);
%>
<c:choose>
<%-- 這是 html 注釋 --%>
<c:when test="${ requestScope.height > 190 }">
<h2>小巨人</h2>
</c:when>
<c:when test="${ requestScope.height > 180 }">
<h2>很高</h2>
</c:when>
<c:when test="${ requestScope.height > 170 }">
<h2>還可以</h2>
</c:when>
<c:otherwise>
<c:choose>
<c:when test="${requestScope.height > 160}">
<h3>大于 160</h3>
</c:when>
<c:when test="${requestScope.height > 150}">
<h3>大于 150</h3>
</c:when>
<c:when test="${requestScope.height > 140}">
<h3>大于 140</h3>
</c:when>
<c:otherwise>
其他小于 140
</c:otherwise>
</c:choose>
</c:otherwise>
</c:choose>
效果展示:
2.4 < c:forEach />
作用:周遊輸出使用。
2.4.1 周遊 1 到 10,輸出
<c:forEach begin="1" end="10" var="i">
<%--1. --%>
${i} <br>
<%--2. --%>
<h1>${i}</h1>
</c:forEach>
或者:
<%--
<c:forEach />
作用:周遊輸出使用
--%>
<%--
1.周遊 1 到 10,輸出
begin 屬性設定開始的索引
end 屬性設定結束的索引
var 屬性表示循環的變量(也是目前正在周遊到的資料)
for (int i = 1; i < 10; i++)
--%>
<table >
<c:forEach begin="1" end="10" var="i">
<tr>
<td>第${i}行</td>
</tr>
</c:forEach>
</table>
2.4.2 周遊 Object 數組
<%--
2.周遊 Object 數組
for (Object item: arr)
items 表示周遊的資料源(周遊的集合)
var 表示目前周遊到的資料
--%>
<%
request.setAttribute("arr", new String[]{"18610541354","18688886666","18699998888"});
%>
<c:forEach items="${ requestScope.arr }" var="item">
${ item } <br>
</c:forEach>
2.4.3 周遊 Map 集合
<%--周遊 Map 集合--%>
<%
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">
<%--輸出所有的鍵值對--%>
${entry}
<%--隻輸出我要的值--%>
${entry.key}
${entry.value}
<%--按照自己希望的格式輸出 key 和 value--%>
<h1>${entry.key} = ${entry.value}</h1>
</c:forEach>
2.4.4 周遊 List 集合—list 中存放 Student 類,有屬性:編号,使用者名,密碼,年齡, 電話資訊
<%--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="2" end="7" 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>${status.step}</td>
</tr>
</c:forEach>
</table>