一、JSTL介紹
JSTL(JavaServer Pages Standard Tag Library)由JCP(Java Community Process)指定標(biāo)準(zhǔn),提供給 Java Web 開發(fā)人員一個(gè)標(biāo)準(zhǔn)通用的標(biāo)簽函數(shù)庫(kù)。和 EL 來取代傳統(tǒng)直接在頁(yè)面上嵌入 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個(gè)文件,myeclipse有自帶的)。
導(dǎo)入標(biāo)簽庫(kù):
例如:
%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
二、JSTL常用標(biāo)簽:
在JSP頁(yè)面中要使用格式化標(biāo)簽,必須使用%@ taglib%>指令,
%@ taglib prefex="c" uri="http://java.sun.com/jsp/jstl/core" %>
1.c:set>標(biāo)簽能夠?qū)⒆兞看鎯?chǔ)在JSP范圍中或者是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ù)存儲(chǔ)到范圍為scope的變量中
(4)
c:set value="value" target="target" property="propertyName">/c:set>
將value值存儲(chǔ)到target對(duì)象的屬性中。
(5)
c:settarget="target" property="propertyName"> 文本內(nèi)容 /c:set>
將文本內(nèi)容的數(shù)據(jù)存儲(chǔ)到target對(duì)象的屬性中
2.c:out>標(biāo)簽用來顯示數(shù)據(jù)的內(nèi)容,其格式語(yǔ)法有四種
(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,存儲(chǔ)在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="${46}"> c:out value="Yes" /> /c:when> c:otherwise> c:out value="No" /> /c:otherwise> /c:choose> /body> /html>
6.c:forEach>標(biāo)簽
一種用來遍歷集合對(duì)象的成員
c:forEach [var="username"] items="collection" [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"] > 本地內(nèi)容 /c:forEach>
一種是用來使語(yǔ)句循環(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)或者是動(dòng)態(tài)的文件包含到本身的JSP網(wǎng)頁(yè)中
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)簽,可以從一個(gè)JSP頁(yè)面跳轉(zhuǎn)到另一個(gè)其他的頁(yè)面上去
不帶參數(shù)的
c:redirect url="url" [context="context"]/>
帶參數(shù)的
c:redirect url="url" [context="context"]>
c:param />標(biāo)簽 /c:redirect>
標(biāo)簽:臨汾 銅川 開封 自貢 山南 烏蘭察布 吉林 白銀
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《JSP中一些JSTL核心標(biāo)簽用法總結(jié)》,本文關(guān)鍵詞 JSP,中,一些,JSTL,核心,標(biāo)簽,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。