一、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 來(lái)取代傳統(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"%>
- uri:用于導(dǎo)入標(biāo)簽庫(kù)的uri。
- prefix:標(biāo)簽庫(kù)的前綴,例如:c:out>,c就是前綴,相當(dāng)于為標(biāo)簽取個(gè)簡(jiǎn)單好記的名字。
- tagdir:指定標(biāo)簽庫(kù)的路徑。
二、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)簽用來(lái)顯示數(shù)據(jù)的內(nèi)容,其格式語(yǔ)法有四種
(1)
c:out value="value">/c:out>
通過(guò)value屬性指定要顯示的值
(2)
c:out value="value" escapeXml="true|false">/c:out>
是否將value中的內(nèi)容按照原樣輸出
(3)
c:out value="value" default="No Data">/c:out>
通過(guò)Default屬性來(lái)設(shè)置默認(rèn)值
(4)
c:out value="value" escapeXml="true|false">
文本內(nèi)容
/c:out>
通過(guò)文本內(nèi)容設(shè)置默認(rèn)的值
3.c:remove>用來(lái)移除指定范圍的變量
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)簽用來(lái)執(zhí)行流程控制
c:if>標(biāo)簽有兩種格式
(1)沒有本體內(nèi)容的
c:if test="condition" var = "varName" [scope="{page|request|session|application}"] />
(2)有本體內(nèi)容的
復(fù)制代碼 代碼如下:
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)簽
一種用來(lái)遍歷集合對(duì)象的成員
c:forEach [var="username"] items="collection" [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"] >
本地內(nèi)容
/c:forEach>
一種是用來(lái)使語(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)簽,用來(lái)根據(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)簽,用來(lái)傳遞參數(shù)
10.c:url>標(biāo)簽,用來(lái)生成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>
您可能感興趣的文章:- JSP中使用JSTL按不同條件輸出內(nèi)容的方法
- JSP入門教程之客戶端驗(yàn)證、常用輸出方式及JSTL基本用法
- jsp 使用jstl實(shí)現(xiàn)翻頁(yè)實(shí)例代碼
- jsp中使用jstl導(dǎo)入html亂碼問(wèn)題解決方法
- JSP和JSTL獲取服務(wù)器參數(shù)示例
- 關(guān)于jsp頁(yè)面使用jstl的異常分析
- JSP中常用的JSTL fmt(format格式化)標(biāo)簽用法整理