(轉)JSTL標籤

1.JSTL1.1標籤庫 
核心標籤庫,國際化標籤庫,數據庫標籤庫,XML標籤庫,函數標籤庫 

2.EL表達式的默認變量 
(1)pageScope,requestScope,sessionScope,applicationScope, 
這個4個變量包含Scope作用範圍得參數集合,相當於保存在java.util.Map種得某個參數 

(2)默認變量param和paramValues 
param表明請求包含的參數爲單一控件,paramValues表明請求包含的參數爲控件數組 

(3)默認變量header和headerValues 
包含請求參數頭部信息得集合,header變量表示單一頭部信息,headerValue則表示數組型得頭部信息 

(4)默認變量cookie 
cookie集合,集合中得每個對象對應javax.servlet.http.Cookie 

(5)默認變量initParam包含所有應用程序初始化參數得集合 

(6)默認變量pageContext 
用來提供訪問不同得請求參數 

EL表達式操作符 
empty:用來對一個空變量進行判斷:null,一個空String,空數組,空map,沒有條目得Collection集合 
func:調用方法,func是方法名,args是參數,可以沒有,或者有一個,多個參數,參數間用都好隔開 

3.JSTL Core標籤庫 
多用途核心標籤:<c:out><c:set><c:remove><c:catch> 
條件控制標籤<c:if><c:choose><c:when><c:otherwise> 
循環控制標籤<c:forEach><c:forToken> 
URL相關標籤<c:import><c:url><c:redirect><c:param> 

<c:out>: 
<c:out value="{$sessonScope.anyValue}" default="no value" excapeXml="false" /> 
從Session查找名爲anyValue的參數,並顯示在頁面,若咩有找到則顯示no value 
xcapeXml當設置爲true時會主動更換特殊字符,比如“&lt;&gt;&amp;”,默認爲true 

<c:set>: 
<c:set value="this is andy" var="oneString" /> 
${oneString} 
將名爲oneString得變量賦值爲this is andy,其作用範圍時page 
target:被賦值的javaBean實例名 
property,javaBean屬性名 
scope:作用範圍,默認爲page 

<c:remove>: 
<c:remove var="sampleValue" scope="session" /> 

<c:catch>: 
<c:catch var="err"> 
${param.smpleSingleValue[9] == 3} 
</c:catch> 
${err} 
從變量var的err中得到異常內容 

<c:if> 
<c:if test="{paramValue.sampleValue[2] == 12}" var"visits" /> 
It is 12 
</c:if> 
${visits} 
將判斷得結果保存在visits中 

<c:choose><c:when><c:otherwise>: 
<c:choose> 
<c:when test="$paramvalues.sampleValue[2] == 11" /> 
not 12 not 13,it is 11 
</c:when> 
<c:when test="$paramvalues.sampleValue[2] == 12" /> 
not 11 not 13,it is 12 
</c:when> 
<c:when test="$paramvalues.sampleValue[2] == 13" /> 
not 11 not 12,it is 13 
</c:when> 
<c:otherwise> 
not 11 12 13 
</c:otherwise> 
</c:choose> 

<c:forEach>: 
<c:forEach items="${seesionScope.arrayList}" var"arrayListI" /> 
${arrayListI} 
</c:forEach> 
var用來接收集合的對象,該循環的變量名 

<c:forTokens>: 
<c:forTokens items="aa,bb,cc,dd" begin="0" end="2" step"2" delims="," var="aValue" /> 
${aValue} 
</c:forTokens> 
結果時"aa cc",跳兩格,截取逗號前面的字符 
delims分隔符, 
varStatus顯示循環狀態得變量 

<c:import> 
<c:import url="/MyHtml.html" var="thisPage" />在同一個Context下導入 
<c:import url="/MyHtml.html" context="/sample2" var="thisPage" />在不同Context下導入 
<c:import url="www.sample.com/MyHtml.html" var="thisPage" />導入任意一個URL 
charEncoding導入頁面的字符集 

<c:url> 
<c:import url="/MyHtml.html" var="urlPage" /> 
<a href="${urlPage}">Link</a> 

<c:redirect>: 
<c:redirect url="/MyHtml.html" /> 
一般和<c:if>等標籤一起使用 

<c:param>: 
<c:redirect url="/MyHtml.html" /> 
<c:param name="userName" value="RW"> 
</c:redirect> 
傳遞參數 


4.JSTL XML processing標籤庫 
XML核心標籤庫:<x:parse><x:out><x:set> 
XML流控制標籤:<x:if><c:choose><c:when><c:otherwise> 
XML轉換標籤<x:transform><x:param> 

解析XML文件的<x:parse> 
<c:import var="xmlFile" url="http://localhost:8080/booksamplejstl/SampleXml.xml"> 
<x:parse var="xmlFileValue" doc="${xmlFile}"> 
varDom:制定保存得變量爲org.w3c.dom.Document接口類型 
scopeDom:org.w3c.dom.Document接口類型的作用範圍 
filter:該屬性必須爲org.xml.sax.XMLFilter類的一個實例,可以使用EL表達式傳入,將對XML文件做過濾得到自身需要的部分 

<x:out> 
<x:parse var="xmlFileValue" doc="${xmlFile}" /> 
name:<x:out select="$xmlFileValue/xml-body/name" /><br> 
password:<x:out select="$xmlFileValue/xml-body/password" /><br> 
age:<x:out select="$xmlFileValue/xml-body/age" /><br> 
加上$作爲XPath表達式得開頭,select中的表達式將從xml-body根元素下得各個子元素中取得屍體內容 

<x:set>: 
<c:set value="this is andy" var="oneString" /> 

<x:if> 
<x:if test="{paramValue.sampleValue[2] == 12}" var"visits" /> 
It is 12 
</x:if> 
${visits} 
將判斷得結果保存在visits中 

<x:choose><c:when><c:otherwise>: 
<x:choose> 
<x:when test="$paramvalues.sampleValue[2] == 11" /> 
not 12 not 13,it is 11 
</x:when> 
<x:when test="$paramvalues.sampleValue[2] == 12" /> 
not 11 not 13,it is 12 
</x:when> 
<x:when test="$paramvalues.sampleValue[2] == 13" /> 
not 11 not 12,it is 13 
</x:when> 
<x:otherwise> 
not 11 12 13 
</x:otherwise> 
</x:choose> 

<x:forEach>: 
<x:forEach items="${seesionScope.arrayList}" var"arrayListI" /> 
${arrayListI} 
</x:forEach> 
var用來接收集合的對象,該循環的變量名 

格式化XML顯示數據得<x:transform>標籤 
<x:transform>標籤允許使用XSLT(轉換XML格式的語言)爲頁面得顯示數據做格式化的處理 
<c:import var="xmlFile" url="http://localhost:8080/booksamplejstl/SampleXml.xml"> 
<c:set var="xsltdoc"> 
<?xml version="1.0" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:template match="/"> 
<xsl:apply-templates /> 
</xsl:template> 
<xsl:template match="xml-body"> 
<html> 
<head></head> 
<body> 
UserName: 
<xsl:value-of select="name" /> 
PassWord: 
<xsl:value-of select="password" /> 
<xsl:value-of select="age" /> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 
</c:set> 

<x:transform xslt="${xsltdoc}" doc="${xmlFile}"> 
將XML文件保存的數據信息利用<c:import>保存在"xmlFile"中,然後使用<c:set>標籤保存在一個XSLT得源到"xsltdoc",最後通過<x:tranform>格式化XML文件得數據顯示在頁面上 


<x:param>: 
<x:redirect url="/MyHtml.html" /> 
<x:param name="userName" value="RW"> 
</x:redirect> 
傳遞參數 


5.I18N fromattion標籤庫 

國際化核心標籤:<fmt:setLocale><fmt:bundle><fmt:setBundle><fmt:message><fmt:param><fmt:requestEncoding> 

格式化標籤:<fmt:timeZone><fmt:setTimeZone><fmt:formatNumber><fmt:parseNumber><fmt:formatDate> 
<fmt:parseDate> 

用於設置本地化環境的<fmt:setLocale> 
<fmt:setLocale value="zh_TW"> 


用於資源文件綁定的<fmt:bundle><fmt:setBundle>標籤 
<fmt:bundle>標籤將資源配置文件綁定於標籤體中得顯示 
<fmt:setBundle>標籤允許將資源配置文件保存爲一個變量,之後的工作可以根據該變量來進行 

<fmt:setBundle basename="applicationMessage" var="applicationBundle" /> 
該實例將會查找一個名爲applicationMessage_zh_CN.properties的資源配置文件,來作爲顯示Resource標籤 
basename:指定資源配置文件,只需要指定文件名而無須擴展名,是兩組標籤的共有屬性 


用於顯示資源配置文件信息的<fmt:message> 
<fmt:setBundle basename="applicationMessage" var="applicationBundle" /> 
<fmt:bundle baseanme="applicationAllMessage"> 
<fmt:message key="userName" /> 
<p> 
<fmt:message key="password" bundle="${applicationBundle}" /> 
</fmt:bundle> 
第一個<fmt:message>標籤將使用"applicationAllMessage"資源配置文件中”鍵“爲"userName"的信息顯示 
第二個<fmt:message>標籤雖然被定義在<fmt:bundle>標籤體內,但是它使用了bundle屬性,因此將指定之前由<fmt:setBundle>標籤保存的"applicationMessage"資源配置文件,該”鍵“爲"passWord"的信息顯示 

用於參數傳遞得<fmt:param>,只有value屬性 

用於爲請求設置字符編碼得<fmt:requestEncoding>標籤,只有value屬性 

用於設定失去得<fmt:timeZone><fmt:setTimeZone> 
<fmt:timeZone>使得在其標籤體內得工作可以用該失去設置 
<fmt:setTimeZone>將失去設置保存爲一個變量 

用於格式化數字得<fmt:formatNumber> 
<fmt:formatNumber value="1000.888" type="currency" var="money"> 
type:格式化類型,由currency(貨幣),number(數字),percent(百分比) 
pattern:格式化模式 
var:結果保存變量 

用於解析數字得<fmt:parseNumber> 
<fmt:parseNumber value="15%" type="precent" var="num"> 
結果是0.15 
type:解析格式化的類型 

用於格式化日期的<fmt:formatDate> 
timeZone:指定格式化日期的時區 
和<fmt:timeZone><fmt:setTimeZone>一起用 

用於解析日期的<fmt:parseDate> 
<fmt:parseDate><fmt:parseNumber>儘量少用,替代工作得地方應該在服務器端表示層的後段,比如在servlet 

6.Database access標籤庫 
用於設置數據源得<sql:setDataSource> 
現在這個標籤已不用,因爲違反了MVC得規範 

7.functions標籤 
爲EL表達式語句提供了許多更有用得功能 
長度函數:fn:length 
字符串處理函數:fn:contains,fn:containsIgnoreCase,fn:endsWith,fn:escapeXml,fn:indexOf,fn:join, 
fn:replace,fn:split,fn:startsWith,fn:substring,fn:substringAfter,fn:substringBefore,fn:toLowerCase,fn:toUpperCase,fn:trim 

fn:length 
${fn:length(sessionScope.arrayList1)} 
參數爲input,將計算通過該屬性傳入的對象長度,該對象應該爲集合類型或String類型,其返回結果是一個int類型的值, 

判斷函數fn:contains 
判斷源字符串是否含有子字符串,返回結果是boolean類型的值 
contains("string","substring"),兩個都是stirng類型 
${fn:contains("ABC","a")}返回false 
${fn:contains("ABC","A")}返回ture 

fn:containslgnoreCase函數 
和fn:contains區別是fn:containslgnoreCase函數的子字符串忽略大小寫 
${fn:ontainslgnoreCase("ABC","a")}返回ture 
${fn:ontainslgnoreCase("ABC","A")}返回ture 

詞頭判斷fn:startsWith 
${fn:startsWith("ABC","ab")}返回false 
${fn:startsWith("ABC","AB")}返回ture 

詞尾判斷fn:endsWith 

字符實體轉換函數fn:escapeXml 
用於將所有特殊字符轉化爲字符實體碼,返回一個string類型 


字符匹配函數fn:indexOf 
${fn:indexOf("ABC","aBC")}返回-1 
${fn:indexOf("ABC","BC")}返回1 
返回-1或1 

分隔符fn:join 
<% String[] stringArray = ("a","b","c"); %> 
<% request.getSession().setAttribute("stringArray",stringArray); %> 
${fn:join(sessionScope.stringArray,",")} 
定義數組並防止到Session中,然後通過Session得到該字符串數組,使用fn:join函數並傳入分隔符";",得到的結果爲"a;b;c" 

替換函數fn:replace 
${fn:replace("ABC","A","B")} 
"ABC"是原字符串,"A"是被替換的字符,"B"替換後的字符 

分隔符轉換數組函數fn:split 
${fn:split("A,B,C",",")} 
將"A,B,C"字符串轉換爲數組{A,B,C} 

字符串截取函數fn:substring函數 
${fn:substring("ABC","1","2")} 
從0開始 
截取結果爲B 

定位到結束截取字符串函數fn:substringAfter函數 
${fn:substringAfter("ABC","BC")},結果爲D(不包括BC) 
允許截取源字符串中某個字符串開始到結束的所有字符 

起始到定位截取字符串函數fn:substringBefore 
${fn:substringAfter("ABC","BC")}結果爲A(不包括BC,BC前的數) 

小寫轉換函數fn:toLowerCase 
${fn:toLowerCase("ABC")} 

大寫轉換函數fn:toUpperCase 
${fn:toLowerCase("abc")} 

空格刪除函數fn:trim函數 
${fn:trim("AB C ")}D 
轉換結果爲"AB CD",只刪除詞尾得空格而不是全部, 


8.<bean:define>,<bean:write>被EL表達式替換 
<bean:cookie><bean:header><bean:parameter><bean:write>被EL表達式替換 
<bean:include>被<c:import>替換 
<bean:message>被<fmt:bundle><fmt:setBundle>,<fmt:message>合作替換 

所有判斷標籤(logic,bean)被EL表達式和<c:if>替換 
<logic:iterate>被<c:forEach>和EL表達替換 
<logic:redirect>被<c:redirect>和<c:param>替換 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章