一、JSTL介紹
JSTL(JavaServer Pages Standard Tag Library)由JCP(Java Community Process)指定標(biāo)準(zhǔn),提供給 Java Web 開發(fā)人員一個標(biāo)準(zhǔn)通用的標(biāo)簽函數(shù)庫。和 EL 來取代傳統(tǒng)直接在頁面上嵌入 Java 程序(Scripting)的做法,以提高程序可讀性、維護(hù)性和方便性。JSTL 主要由Apache組織的Jakarta Project 實(shí)現(xiàn),容器必須支持Servlet 2.4 且JSP 2.0 以上版本。
JSTL下載地址:http://tomcat.apache.org/taglibs/standard/,最新版本為JSTL 1.2,本文下載的是JSTL1.1
安裝:
解壓jakarta-taglibs-standard-1.1.2.zip,將解壓后lib目錄下的jstl.jar,standard.jar直接拷貝到工程下的WEB-INF/lib/目錄下(如果用的是myeclipse可以不用復(fù)制這2個文件,myeclipse有自帶的)。
導(dǎo)入標(biāo)簽庫:
例如:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- uri:用于導(dǎo)入標(biāo)簽庫的uri。
- prefix:標(biāo)簽庫的前綴,例如:<c:out>,c就是前綴,相當(dāng)于為標(biāo)簽取個簡單好記的名字。
- tagdir:指定標(biāo)簽庫的路徑。
二、JSTL常用標(biāo)簽:
在JSP頁面中要使用格式化標(biāo)簽,必須使用<%@ taglib%>指令,
<%@ taglib prefex="c" uri="http://java.sun.com/jsp/jstl/core" %>
1.<c:set>標(biāo)簽能夠?qū)⒆兞看鎯υ贘SP范圍中或者是JavaBean的屬性中
有五種格式:
(1)
<c:set var="username" value="value"></c:set>
制定變量名和變量值
(2)
<c:set var="username" value="value" scope="page|request|session|application"></c:set>
將value值保存到范圍為scope的變量中
(3)
<c:set var="username" scope="page|request|session|application" >文本內(nèi)容</c:set>
將文本內(nèi)容的數(shù)據(jù)存儲到范圍為scope的變量中
(4)
<c:set value="value" target="target" property="propertyName"></c:set>
將value值存儲到target對象的屬性中。
(5)
<c:settarget="target" property="propertyName"> 文本內(nèi)容</c:set>
將文本內(nèi)容的數(shù)據(jù)存儲到target對象的屬性中
2.<c:out>標(biāo)簽用來顯示數(shù)據(jù)的內(nèi)容,其格式語法有四種
(1)
<c:out value="value"></c:out>
通過value屬性指定要顯示的值
(2)
<c:out value="value" escapeXml="true|false"></c:out>
是否將value中的內(nèi)容按照原樣輸出
(3)
<c:out value="value" default="No Data"></c:out>
通過Default屬性來設(shè)置默認(rèn)值
(4)
<c:out value="value" escapeXml="true|false">文本內(nèi)容</c:out>
通過文本內(nèi)容設(shè)置默認(rèn)的值
3.<c:remove>用來移除指定范圍的變量
<c:remove var="number" scope="session"><%@page language="java" contentType="text/html;charset=gb2312" %><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!DOCTYPE html><html><head><title>remove標(biāo)簽的使用</title></head><body><h1>remove標(biāo)簽的使用</h1><hr /><%-- 添加變量number,存儲在page范圍 --%><c:set var = "number" value="${1+2}" scope="page" /><%-- 輸出變量 --%>number:<c:out value="${pageScope.number}" default="No Data"/><%-- 移除page范圍number變量 --%><c:remove var="number" scope="page" /><%-- 輸出變量 --%>number:<c:out value="${pageScope.number}" default="No Data"/></body></html>
4.<c:if>標(biāo)簽用來執(zhí)行流程控制
<c:if>標(biāo)簽有兩種格式
(1)沒有本體內(nèi)容的
<c:if test="condition" var = "varName" [scope="{page|request|session|application}"] />
(2)有本體內(nèi)容的
<c:if test="condition" var = "varName" [scope="{page|request|session|application}"] >本體內(nèi)容</c:if>
5.<c:choose><c:when><c:otherwise>標(biāo)簽
<%@page language="java" contentType="text/html;charset=gb2312" %><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!DOCTYPE html><html><head><title>Choose標(biāo)簽的使用</title></head><body><h1>Choose標(biāo)簽的使用</h1><hr /><c:choose><c:when test="${4<6}"><c:out value="Yes" /></c:when><c:otherwise><c:out value="No" /></c:otherwise></c:choose></body></html>
6.<c:forEach>標(biāo)簽
一種用來遍歷集合對象的成員
<c:forEach [var="username"] items="collection" [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"] >本地內(nèi)容</c:forEach>
一種是用來使語句循環(huán)執(zhí)行指定的次數(shù)
<c:forEach [var="username"] [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"] >本地內(nèi)容</c:forEach>
7.<c:forTokens>標(biāo)簽,用來根據(jù)指定分隔符分割字符串
<c:forTokens [var="varname"] items="stringOfTokens" delims="delimiters" [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"] >本地內(nèi)容</c:forEach>
8.<c:import>標(biāo)簽,可以把靜態(tài)或者是動態(tài)的文件包含到本身的JSP網(wǎng)頁中
<c:import url="url" [context="context"][var="varname"] [scope = "{page|request|session|application}"] [charEncoding="charEncoding"] >本地內(nèi)容</c:import>
9.<c:param>標(biāo)簽,用來傳遞參數(shù)
10.<c:url>標(biāo)簽,用來生成URL
不帶參數(shù)的
<c:url value="value" [context="context"][var="varname"] [scope = "{page|request|session|application}"] />帶參數(shù)的<c:url url="url" [context="context"][var="varname"] [scope = "{page|request|session|application}"] >
<c:param />標(biāo)簽</c:url>
11.<c:redirect>標(biāo)簽,可以從一個JSP頁面跳轉(zhuǎn)到另一個其他的頁面上去
不帶參數(shù)的
<c:redirect url="url" [context="context"]/>
帶參數(shù)的
<c:redirect url="url" [context="context"]>
<c:param />標(biāo)簽</c:redirect>