JavaWeb學習筆記——JSTL標籤庫
阿新 • • 發佈:2020-12-25
JSTL標籤庫
介紹
JSTL 標籤庫,JSP Standard Tag Library,JSP 標準標籤庫
EL 表示式替換了 jsp 頁面的表示式指令碼,JSTL 標籤庫則替換了 jsp 頁面的程式碼指令碼,這樣使得整個 jsp 頁面
變得更佳簡潔。
標籤庫
JSTL 由五個不同功能的標籤庫組成。
使用方法
- 匯入 JSTL 標籤庫的 jar 包。
taglibs-standard-impl-1.2.1.jar
taglibs-standard-spec-1.2.1.jar - 使用 taglib 指令在 jsp 頁面中匯入標籤庫。
core 標籤庫
<c:set />(使用很少)
作用:向域中儲存資料
- scope屬性: 設定儲存到哪個域
page:表示PageContext域(預設值)
request:表示Request域
session:表示Session域
application:表示ServletContext域 - var屬性:設定key
- value屬性:設定值
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%--
set標籤:<c:set>
作用:向域中儲存資料
scope屬性: 設定儲存到哪個域
page:表示PageContext域(預設值)
request:表示Request域
session:表示Session域
application:表示ServletContext域
var屬性:設定key
value屬性:設定值
-- %>
<c:set scope="request" var="a" value="123"></c:set>
${ requestScope.a }
</body>
</html>
<c:if />
作用:進行 if 判斷
test屬性:表示判斷的條件(使用 EL表示式)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:set scope="request" var="a" value="123"></c:set>
<%--
if標籤:<c:if>
作用:進行if判斷
test屬性:表示判斷的條件(使用 EL表示式)
--%>
<c:if test="${ requestScope.a == 123 }">
<h2>JSTL的if標籤</h2>
</c:if>
</body>
</html>
<c:choose> <c:when> <c:otherwise>
作用:多路判斷,與 switch … case … default 相似
- choose標籤:多路判斷的開始標籤
- when標籤:表示每一種情況
test屬性:表示當前這種情況要滿足的條件 - otherwise標籤:表示剩下的情況
注意:
- 標籤裡不能使用 html 註釋,要使用 jsp 註釋
- when標籤的父標籤必須是 choose 標籤
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%--
<c:choose> <c:when> <c:otherwise>標籤:
作用:多路判斷,與switch ... case .... default相似
choose標籤:多路判斷的開始標籤
when標籤:表示每一種情況
test屬性:表示當前這種要滿足的條件
otherwise標籤:表示剩下的情況
注意:
1、標籤裡不能使用html註釋,要使用jsp註釋
2、when標籤的父標籤必須是choose標籤
--%>
<c:set scope="request" var="height" value="178"></c:set>
<c:choose>
<c:when test="${ requestScope.height >= 180 }">
<div>身高大於180cm</div>
</c:when>
<c:when test="${ requestScope.height < 180 && requestScope.height >= 170 }">
<div>身高在170-180cm之間</div>
</c:when>
<c:otherwise>
<div>身高小於170cm</div>
</c:otherwise>
</c:choose>
</body>
</html>
<c:forEach />
常規遍歷
- begin屬性:設定開始索引
- end屬性:設定結束索引
- var屬性:表示迴圈的變數,即當前正在遍歷的資料
- step屬性:步長
<body>
<%--
<c:forEach>標籤 1:
作用 :遍歷——常規遍歷
begin屬性:設定開始索引
end屬性:設定結束索引
var屬性:表示迴圈的變數,即當前正在遍歷的資料
step屬性:步長
--%>
<%-- 遍歷輸出1-10 --%>
<c:forEach begin="1" end="10" var="i">
<div>${ i }</div><br>
</c:forEach>
<%-- 輸出2行5列的表格 --%>
<table border="1">
<c:forEach begin="1" end="2" var="i">
<tr>
<c:forEach begin="1" end="5" var="j">
<td>第${ i }行第${ j }個單元格</td>
</c:forEach>
</tr>
</c:forEach>
</table>
</body>
遍歷陣列
- item屬性:表示遍歷的資料來源(遍歷的陣列)
- var屬性:表示迴圈的變數,即當前正在遍歷的資料
<body>
<%--
<c:forEach>標籤 2:
作用 :遍歷——遍歷陣列
item屬性:表示遍歷的資料來源(遍歷的陣列)
var屬性:表示迴圈的變數,即當前正在遍歷的資料
--%>
<%
request.setAttribute("arr", new String[]{"123", "456", "789"});
%>
<c:forEach items="${ requestScope.arr }" var="item">
${ item }<br>
</c:forEach>
</body>
遍歷Map集合
- item屬性:表示遍歷的資料來源
- var屬性:表示迴圈的變數,即當前正在遍歷的資料
<body>
<%--
<c:forEach>標籤 3:
作用 :遍歷——遍歷Map集合
item屬性:表示遍歷的資料來源(遍歷的陣列)
var屬性:表示迴圈的變數,即當前正在遍歷的資料
遍歷Map集合之後得到EntrySet集合,屬性key可以獲得全部鍵,屬性value可以獲得全部值
(注意:獲取Map集合中的某個值時,map.key1獲取的是key=key1的value值)
--%>
<%
Map<String, Object> map = new HashMap<String, Object>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
request.setAttribute("map", map);
%>
<c:forEach items="${ requestScope.map }" var="entry">
鍵${ entry.key }值${ entry.value }<br>
</c:forEach>
</body>
遍歷List集合
- item屬性:表示遍歷的資料來源
- var屬性:表示迴圈的變數,即當前正在遍歷的資料
<body>
<%--
<c:forEach>標籤 4:
作用 :遍歷——遍歷List集合
item屬性:表示遍歷的資料來源(遍歷的陣列)
var屬性:表示迴圈的變數,即當前正在遍歷的資料
需求:
建立10個學生物件,包括id,username,password,age,phone,
儲存到List集合中,之後遍歷輸出
--%>
<%
List<Student> stuList = new ArrayList<Student>();
for(int i=1; i<=10; i++){
stuList.add(new Student(i, "username"+i, "password"+i, 18+i, "phone"+i));
}
request.setAttribute("student", stuList);
%>
<table>
<tr>
<th>編號</th>
<th>姓名</th>
<th>密碼</th>
<th>年齡</th>
<th>電話</th>
</tr>
<c:forEach items="${ requestScope.student }" var="stu">
<tr>
<td>${ stu.id }</td>
<td>${ stu.username }</td>
<td>${ stu.password }</td>
<td>${ stu.age }</td>
<td>${ stu.phone }</td>
</tr>
</c:forEach>ach>
</table>
</body>
其他
<body>
<%--
<c:forEach>標籤 5:
varStatus屬性:當前遍歷到的資料的狀態
getCurrent:獲取當前遍歷到的元素
getIndex:獲取當前遍歷到的元素的索引
getCount:當前遍歷到的元素的個數
isFirst:判斷當前元素是否是第一個元素
isLast:判斷當前元素是否是最後一個元素
getBegin:獲取begin屬性值
getEnd:獲取end屬性值
getStep:獲取step屬性值
--%>
</body>