欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JSP中一些JSTL核心標(biāo)簽用法總結(jié)

 更新時(shí)間:2016年04月14日 15:04:00   作者:張大鵬  
這篇文章主要介紹了JSP中一些JSTL核心標(biāo)簽用法總結(jié),JSTL標(biāo)簽可以用來方便地操作變量并且還支持自定義功能,需要的朋友可以參考下

一、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ù)庫。和 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個(gè)文件,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)簽取個(gè)簡單好記的名字。
  • 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ū)⒆兞看鎯?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對象的屬性中。

(5)

<c:settarget="target" property="propertyName">
 
文本內(nèi)容

</c:set>

將文本內(nèi)容的數(shù)據(jù)存儲(chǔ)到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,存儲(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)容的

復(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="${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)或者是動(dòng)態(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)簽,可以從一個(gè)JSP頁面跳轉(zhuǎn)到另一個(gè)其他的頁面上去

不帶參數(shù)的

<c:redirect url="url" [context="context"]/>

帶參數(shù)的

<c:redirect url="url" [context="context"]>

<c:param />標(biāo)簽

</c:redirect>

相關(guān)文章

最新評論