WEB專案-JSTL標籤庫
JSTL標籤庫
1、JSTL:JSP Standard Tag Library ,JSP標準標籤庫,JSP的內建標籤
2、JSP的標籤執行在伺服器(Tomcat)中,和HTML標籤不同
3、JSP標籤的出現是為了簡化程式設計,去除掉<% %>方式。底層也是Java程式碼
4、
5、引入jstl標籤庫,使用taglib指令
<%@ taglib prefix=" " uri=" "%>
<!-- 引入了JSTL標籤庫 --> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
使用必須先使用字首:<c:標籤名 屬性>輸出內容(可以直接是HTML標籤)</c:標籤名>
6、JSTL核心標籤
- 核心標籤庫(core)--- c
- fn.tld --- EL函式庫
7、EL表示式和JSTL標籤操作的都是域物件中的屬性和屬性值
out標籤
1、作用:輸出域物件的值或者常量
2、屬性
- value -- 輸出的內容
- default -- 預設值
- escapeXml -- 設定預設轉義,預設是轉義
<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h4>JSTL方式</h4> <% int a = 10; request.setAttribute("num", a); %> <c:out value="${ requestScope.num }"></c:out> <c:out value="11"></c:out> <!-- default 設定輸出的預設值 --> <c:out value="${ num2 }" default="預設預設"></c:out> <!-- escapeXml屬性 --> <c:out escapeXml="false" value="<a href='http://www.baidu.com'>百度</a>"></c:out> </body> </html>
set標籤
1、作用:向域中存入值
2、<c:set >
3、屬性
- var -- 屬性名稱
- value -- 屬性值
- scope -- 域範圍
<c:set var="username" value="zhangsan" scope="page"></c:set>
<c:out value="${ pageScope.username }"></c:out>
${ pageScope.username }
if標籤
1、if標籤可以用來判斷
2、屬性
- test -- 判斷條件返回true或者false
- var -- test返回的結果儲存到var中
- scope -- 域的範圍
<c:set var="num" value="10" scope="page"></c:set>
<c:if test="${ pageScope.num eq 10 }" var="i" scope="page">
num = 10
${ pageScope.i }
</c:if>
choose標籤
1、作用:判斷資料
2、語法:
<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>JSTL方式</h4>
<c:set var="num" value="10" scope="page"></c:set>
<c:choose>
<c:when test="${ pageScope.num >= 10 }">
num>=10
</c:when>
<c:when test="${ pageScope.num < 10 }">
num<10
</c:when>
<c:otherwise>
其他
</c:otherwise>
</c:choose>
</body>
</html>
foreach標籤
1、迭代資料。遍歷list集合
2、語法:<c:forEach>
3、屬性:
和java中的foreach比較
for(String str : 集合(strs))
- var -- 迴圈遍歷的資料儲存到var中,相當於str變數
- intems -- 相當於集合(strs),從域物件中取值
普通for迴圈for(int i = 0;i<10;i++){}
- begin -- 迴圈從哪裡開始 int i = 0
- end -- 迴圈到哪裡結束 i < 10
- step -- 步長 i++
- varStatus -- 迭代相關的資訊
-- index -- 下標值
-- count -- 計數器
-- first -- 判斷是否是第一個成員
-- last -- 判斷是否是最後一個成員
案例:將使用者列表顯示在頁面上
<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>JSTL方式</h4>
<h4>獲取request域中的list集合</h4>
<c:forEach var="user" items="${ requestScope.ulist }">
${ user.username }
${ user.password }
</c:forEach>
<h4>使用者的資訊列表</h4>
<table border="1" width="60%" cellpadding="10">
<tr>
<th>序號</th>
<th>姓名</th>
<th>密碼</th>
<th>操作</th>
</tr>
<c:forEach var="user" items="${ requestScope.ulist }" varStatus="s">
<tr align="center">
<td>${ s.count }</td>
<td>${ user.username }</td>
<td>${ user.password }</td>
<td><a href="#">刪除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
url標籤
1.作用:有提交的地址相關。(超連結 form表單 img的src屬性)
2.好處:假如你使用form的action的編寫地址
<form action="${pageContext.request.contextPath}/xxx">
如果使用<c:url的標籤> <form action="<c:url value='/xxx' >">
3.特點:如果不寫專案名,預設給你加上專案名稱。
4.好處:使用<c:url>,預設給你拼接jsessionid session的追蹤(在請求的地址預設給你jsessionid=XCSDFSDFSD)
remove標籤
1、作用:刪除域物件中的屬性
2、<c:remove >
- var -- 要刪除屬性的名稱 <c:remove var="${ xx }" scope="page"></c:remove>
- scope -- 域範圍
catch標籤
1、作用:捕獲異常
2、<c:catch >
3、屬性
- var -- 如果發生了異常,把異常的資訊儲存到var變數中