前言
=========================================================================
jstl标簽庫,是日常開發經常使用的,也是衆多标簽中性能最好的。把常用的内容,放在這裡備份一份,随用随查。盡量做到不用查,就可以随手就可以寫出來。這算是java程式員的基本功吧,一定要紮實。
jstl全名為javaserver pages standard tag library,目前最新的版本為1.1版。jstl是由jcp(java
community process)所制定的标準規範,它主要提供給java
web開發人員一個标準通用的标簽函數庫。
web程式員能夠利用jstl和el來開發web程式,取代傳統直接在頁面上嵌入java程式(scripting)的做法,以提高程式的閱讀性、維護性和友善性。
jstl
1.1必須在支援servlet 2.4且jsp 2.0以上版本的container才可使用
<%@ taglib %>引入标簽庫
1、以classpath中,加入jar包: standard-1.1.2.jar , jstl-1.1.2.jar
2、在相目\web-inf\tld\檔案夾中放入常用的tld檔案:c.tld,fmt.tld
3、在jsp檔案的頂部加入以下内容:
java代碼
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<c:> 核心标簽庫
==========================================================================
核心标簽庫(c标簽)标簽共有13個,功能上分為4類:
1.表達式控制标簽:out、set、remove、catch
2.流程控制标簽:if、choose、when、otherwise
3.循環标簽:foreach、fortokens
4.url操作标簽:import、url、redirect
<c:foreach> 标簽
-------------------------------------------------------------------
為循環控制,它可以将集合(collection)中的成員循序浏覽一遍。
<c:foreach>
标簽的文法 說明 :
文法1:疊代一集合對象之所有成員
html代碼
<c:foreach [var="varname"] items="collection" [varstatus="varstatusname"] [begin="begin"] [end="end"] [step="step"]>
本體内容
</c:foreach>
文法2:疊代指定的次數
<c:foreach [var="varname"] [varstatus="varstatusname"] begin="begin" end="end" [step="step"]>
<c:foreach>
标簽的 屬性說明 :
<c:foreach> 标簽的 屬性
: varstatus屬性: 它的提供另外四個屬性:index,count,fist和last,它們個自的意義如下:
屬性 類型 意義
index number 現在指到成員的索引
count number 總共指到成員的總和
first boolean 現在指到成員是否為第一個
last boolean 現在指到成員是否為最後一個
<c:foreach> 周遊 list清單:
對于一個基本類型的數組,目前元素将作為相應包裝類(integer、float等等)的一個執行個體提供。
<c:foreach items="${domainlist }" var="item">
<tr>
<td align="center" valign="middle">${item["domain"]==null?"&nbsp;":item["domain"]}</td>
<td align="center" valign="middle"><fmt:formatdate value="${item[‘bind_date‘]}" pattern="yyyy-mm-dd hh:mm:ss"/></td>
<td align="center" valign="middle">
<c:if test="${item[‘domain‘]!=null}">
<a href="javascript:;" id="${item[‘domain‘]}" class="del">&nbsp;</a>
</c:if>
</td>
</tr>
<c:foreach> 周遊map:
對于一個java.util.map,目前元素則作為一個java.util.map.entry提供。
<c:if test="${!empty permissionmap}">
<c:foreach items="${permissionmap}" var="item">
<td>${item.value.id}</td>
<td>${item.value.urlonclass}</td>
<td>${item.value.urlonmethod}</td>
</tr>
</c:if>
<c:fortokens> 标簽
用來浏覽一字元串中所有的成員,其成員是由定義符号(delimiters)所分隔的。
<c:fortokens>
<c:fortokens items="stringoftokens" delims="delimiters" [var="varname"]
[varstatus="varstatusname"] [begin="begin"] [end="end"] [step="step"]>
</c:fortokens>
<c:fortokens>
<c:out> 标簽
主要用來顯示資料的内容
<c:out>
文法1:沒有本體(body)内容
<c:out value="value" [escapexml="{true|false}"] [default="defaultvalue"] />
文法2:有本體内容
<c:out value="value" [escapexml="{true|false}"]>
default value
</c:out>
标簽的 屬性說明 : 略
一般來說,<c:out>預設會将<、>、’、” 和 & 轉換為
&lt;、&gt;、&#039;、&#034;
和&amp;。假若不想轉換時,隻需要設定<c:out>的escapexml屬性為fasle就可以了。
<c:set> 标簽
主要用來将變量儲存至jsp範圍中或是javabean的屬性中。
<c:set>
文法1:将value的值儲存至範圍為scope的 varname 變量之中
<c:set value="value" var="varname" [scope="{ page|request|session|application }"]/>
文法2:将本體内容的資料儲存至範圍為scope的 varname 變量之中
<c:set var="varname" [scope="{ page|request|session|application }"]>
… 本體内容
</c:set>
文法3:将 value的值儲存至 target 對象的屬性中
<c:set value="value" target="target" property="propertyname" />
文法4:将本體内容的資料儲存至target 對象的屬性中
<c:set target="target" property="propertyname">
<c:set>
<c:remove> 标簽
主要用來移除變量。
<c:remove>
<c:remove var="varname" [scope="{ age|request|session|application }"] />
<c:catch> 标簽
主要用來處理産生錯誤的異常狀況,并且将錯誤資訊儲存起來。
<c:catch>
<c:catch [var="varname"] >
… 欲抓取錯誤的部分
</c:catch>
<c:if> 标簽
的用途就和我們一般在程式中用的if一樣。
<c:if>
文法1:沒有本體内容(body)
<c:if test="testcondition" var="varname" [scope="{page|request|session|application}"]/>
<c:if test="testcondition" [var="varname"] [scope="{page|request|session|application}"]>
</c:if>
示例:
<c:if test="${not empty item.publish_time}">
内容
<c:if test="${item[‘domain‘]!=null}">
<c:if test="${!empty permissionmap}">
c:choose> <c:when>
<c:otherwise> 标簽
<c:choose when otherwise>
<c:set var="score">85</c:set>
<c:choose>
<c:when test="${score>=90}">
你的成績為優秀!
</c:when>
<c:when test="${score>=70&&score<90}">
您的成績為良好!
<c:when test="${score>60&&score<70}">
您的成績為及格
<c:otherwise>
對不起,您沒有通過考試!
</c:otherwise>
</c:choose>
<fmt:> 格式
化标簽庫
一:jstl格式化标簽又稱為i18n标簽庫,主要用來編寫國際化的web應用,使用此功能可以對一個特定的語言請求做出合适的處理。
例如:中國内地使用者将顯示簡體中文,台灣地區則顯示繁體中文,使用i18n格式化标簽庫還可以格式化數字和日期,例如同一數字或日趨,在不同國家可能有不同的格式,使用i18n格式标簽庫可以将數字和日期格式為當地的格式。
在jsp頁面中要使用到格式化标簽,需要引入下面的語句:
<%@
taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%
>
二:概覽
格式化标簽
<fmt:fromatnumber>
<fmt:formatdate>
<fmt:parsedate>
<fmt:parsenumber>
<fmt:settimezone>
<fmt:timezone>
國際化标簽
<fmt:setlocale>
<fmt:requestencoding>
<fmt:bundle>
<fmt:message>
<fmt:param>
<fmt:setbundle>
三:<fmt:formatnumber>
此标簽會根據區域定制的方式将數字格式化成數字,貨币,百分比。
此标簽的屬性:
value:要格式化的數字
type:按照什麼類型格式化
pattern:自定義格式化樣式
currencycode:iso-4721貨币代碼,隻适用于按照貨币格式化的數字
currencysymbol:
貨币符号,如¥,隻适用于按照貨币格式化的數字
groupingused:
是否包含分隔符
maxintegerdigits:
整數部分最多顯示多少位
mixintegerdigits:
整數部分最少顯示多少位
maxfractiondigits:
小數部分最多顯示多位位
minfractiondigits:
小數部分最少顯示多位位
var:存儲格式化後的結果
scope:
存儲的範圍
示例1:
<%@ page language="java"
pageencoding="utf-8"%>
<%@ taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"
%>
<!doctype html public "-//w3c//dtd html 4.01
transitional//en">
<html>
<head>
<title>chapter4.jsp</title>
</head>
<body>
<div>
<fmt:setlocale
value="fr_fr"/>
<fmt:formatnumber
value="123456789.012"/>
<br/>
value="zh_cn"/>
<br
/>
value="de_de"/>
/>
</div>
</body>
</html>
<%@ page
language="java" pageencoding="utf-8"%>
uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!doctype html
public "-//w3c//dtd html 4.01
transitional//en">
<html>
<head>
<title>chapter4.jsp</title>
</head>
<body>
<div>
<fmt:setlocale
value="fr_fr"/>
<fmt:formatnumber
value="123456789.012"/>
<br/>
<fmt:setlocale
value="zh_cn"/>
<br />
value="de_de"/>
<br />
</div>
</div>
</body>
</html>
注意:如果要實作國際化,那麼編碼格式要設定為utf-8.
從程式運作效果可以看出,設定的區域不同,格式化數字的顯示也會不同.
四:type屬性:可以是數字(number),貨币(currency),百分比(percent)
示例2:
<fmt:formatnumber value="0.3" type="number"/><br
<fmt:formatnumber value="0.3" type="currency"/><br
<fmt:formatnumber value="0.3" type="percent"/><br
/>
<fmt:formatnumber value="0.3" type="number"/><br
/>
<fmt:formatnumber value="0.3" type="currency"/><br
<fmt:formatnumber value="0.3" type="percent"/><br
/>
</html>
currencycode為貨币代碼,例如美元為usd,人民币為cny等
currencysymbol為貨币符号例如,人民币為¥,美元為$。
如果不指定區域,則會根據語言區域自動選擇currencysymbol
示例3:
value="en_us"/>
/>
<fmt:setlocale value="en_us"/>
<fmt:formatnumber
value="0.3" type="currency"/><br />
</div>
</html>
currencysymbol屬性還可以自定義要顯示的頭辨別,但是一定得type="currency"才會生效,例如:
page language="java" pageencoding="utf-8"%>
taglib uri="http://java.sun.com/jsp/jstl/core"
<fmt:formatnumber value="0.3" type="currency" currencysymbol="#"/><br
/>
<fmt:formatnumber value="0.3" type="currency"
currencysymbol="#"/><br />
value="en_us"/>
currencysymbol="#"/><br />
自定義數字樣式
<fmt:formatnumber value="12.31"
pattern=".0000"/><br/>
value="1234" pattern="###.##e0"/>
會顯示:
12.3100
1.234e3
會四舍五入
var:定義一個變量,存儲格式化後的結果,scope指定變量存儲的範圍.用法和前面講的标簽一緻.
五:<fmt:parsenumber>
此标簽用來将字元串類型的數字,貨币或百分比轉換成數字類型,和<fmt:formatnumber>标簽的作用正好相反.
value: 要轉換的字元串
type:
指定要轉換的字元串為什麼類型,可取值:number,percent,currency
pattern:
自定義格式化樣式
parselocale:
指定區域來轉換字元串
integeronly:
轉換後的數字是否隻顯示整數部分
var:
存儲轉換後的結果
scope: 存儲的範圍
示例1:
<fmt:parsenumber
value="500,800,200"/>
顯示:
500800200
示例2:
<fmt:parsenumber value="52%" type="percent" />
0.52 (52%在這裡是一個字元串, type指定這個字元串是什麼類型的值)
示例3:
<fmt:parsenumber value="¥123" type="currency"
顯示123, ¥123在這裡是一個字元串,
type指定這個字元串是什麼類型的值
示例4:
<fmt:parsenumber value="123.333" type="number"
/><br/>
<fmt:parsenumber value="123.333"
type="number"
integeronly="true"/><br/>
顯示:
123.333
123
integeronly确定是否隻顯示整數部分.
示例5:
<fmt:parsenumber value="¥123.333" type="currency"
parselocale="zh_cn"/><br/>
<fmt:parsenumber
value="$123.333" type="currency"
parselocale="en_us"/><br/>
parselocale="en_us"主要是配合當type="currency"時用的,
如果要轉換貨币的字元串類型為value="¥123.333",不設定語言環境的話,會取目前浏覽器的預設設定,否則就要加上parselocale="zh_cn",指定環境為中文環境
如果要轉換貨币的字元串類型為value="$123.333",不設定語言環境的話,會取目前浏覽器的預設設定,如果預設為zh_cn的話,程式會報錯的,否則就要加上parselocale="en_us",指定環境為英文美國環境
六:<fmt:formatdate
此标簽可以将日期格式化.
屬性介紹:
value
用來格式化的時間或日期
type
指定格式化的是日期還是時間,或者兩者都是取值範圍:date,time,both
pattern
datestyle
日期的格式化樣式
timestyle
時間的格式化樣式
timezone
指定使用的時區
var 存儲格式化後的結果
scope 指定存儲的範圍
自定義格式:
--------------------------
<fmt:setlocale value="zh_cn"
<fmt:formatdate value="<%=new date()%>"
type="both" pattern="yyyy/mm/dd hh:mm:ss" />
type="both" pattern="yyyy-mm-dd hh:mm:ss" />
type="both" pattern="yyyy年mm月dd日 hh小時mm分鐘ss秒" />
type="both" pattern="yy/mm/dd hh:mm:ss" />
/>
<fmt:formatdate value="<%=new date()%>" type="both"
pattern="yyyy/mm/dd hh:mm:ss" />
<br />
<fmt:formatdate
value="<%=new date()%>" type="both" pattern="yyyy-mm-dd hh:mm:ss"
type="both" pattern="yyyy年mm月dd日 hh小時mm分鐘ss秒" />
pattern="yy/mm/dd hh:mm:ss" />
<br /> 注意這裡小時
hh表示12小時制, hh代表24小時制
示例1:
value="zh_cn" />
<fmt:formatdate value="<%=new
date()%>" />
<fmt:setlocale value="zh_tw"
<fmt:formatdate value="<%=new date()%>" />
<fmt:setlocale value="zh_tw" />
value="<%=new date()%>"
/>
大家可以看到大陸和台灣顯示日期的格式是有差別的.
顯示結果:
2009-12-7
2009/12/7
date()%>" type="time"/>
type="time"/>
type="time"/>
type="time"/>
14:59:28
下午 02:59:28
type可取值及意義:
date 格式化日期
time格式化時間
both格式化日期時間
示例3:
type="both" />
type="both" />
輸出結果:
2009-12-7
21:24:26
2009/12/7 下午 09:24:26
datestyle用來設定日期顯示的樣式,其值可以是default, short, medium, long,
full,請看示例:
date()%>" type="both" datestyle="default" />
type="both" datestyle="short" />
type="both" datestyle="medium" />
type="both" datestyle="long" />
type="both" datestyle="full" />
datestyle="default" />
date()%>" type="both" datestyle="short" />
datestyle="medium" />
date()%>" type="both" datestyle="long" />
datestyle="full" />
顯示結果如下:
2009-12-7
21:30:49
09-12-7
2009年12月7日
2009年12月7日 星期一
21:30:49
可以看到datestyle屬性隻對日期部分起作用,時間部分沒有作用.
timestyle用來顯示時間部分的樣式,取值範圍同上
date()%>" type="both" timestyle="default"
<br />
value="<%=new date()%>" type="both" timestyle="short"
value="<%=new date()%>" type="both" timestyle="medium"
value="<%=new date()%>" type="both" timestyle="long"
value="<%=new date()%>" type="both" timestyle="full"
<br />
value="zh_cn" />
type="both" timestyle="default" />
timestyle="short" />
date()%>" type="both" timestyle="medium" />
timestyle="long" />
date()%>" type="both" timestyle="full" />
輸出:
21:35:52
下午9:35
下午09時35分52秒
2009-12-7 下午09時35分52秒
cst
timezone用來設定時區,時區的意思類似于酒店裡大堂放的幾個時鐘,比如現在時間會有中原標準時間,東京時間,紐約時間,倫墩時間,
取值範圍為:est, cst, mst,
pst
date()%>" type="time" timestyle="full" />
type="time" timestyle="full" timezone="est" />
type="time" timestyle="full" timezone="cst" />
type="time" timestyle="full" timezone="mst" />
type="time" timestyle="full" timezone="pst" />
<fmt:formatdate value="<%=new date()%>" type="time"
timestyle="full" />
date()%>" type="time" timestyle="full" timezone="est" />
timestyle="full" timezone="cst" />
value="<%=new date()%>" type="time" timestyle="full" timezone="mst"
type="time" timestyle="full" timezone="pst" />
/> 輸出結果:
下午09時41分37秒
cst
上午08時41分37秒 est
上午07時41分37秒 cst
上午06時41分37秒
mst
上午05時41分37秒
pst
七:<fmt:parsedate>
将字元串類型的時間轉換為日期類型.
value
用來格式化的時間或日期的字元串
type
pattern
datestyle 日期的格式化樣式
timestyle 時間的格式化樣式
timezone 指定使用的時區
var 存儲格式化後的結果
scope 指定存儲的範圍
示例:
<fmt:setlocale value="zh_cn"
<fmt:parsedate type="date"
value="2008-4-5"/>
輸出: sat apr 05 00:00:00 cst
2008,
這裡已經将字元串”
2008-4-5”轉換為了日期對象了.轉換一定得注意,類似于2008-4-5這樣的字元串,type必須為date,類似于12:34:56的字元串,type必須為time類似于2008-4-5
12:34:56這樣的字元串,type必須為both還要注意浏覽器的語言環境的設定,如果為zh_tw,那麼字元串就必須得符合當地的标準,如為2009/12/7
下午
09:24:26就正确轉換為日期對象,否則就會報錯.
八:<fmt:settimezone>
value 設定時區
var 存儲設定的時區
scope
value用來設定時區,可以是est,cst,mst,pst等,如果有var屬性,則将結果存儲在所設定的範圍之内.在屬性範圍内的頁面都會使用該時區為預設時區.
<fmt:settimezone value="est"
type="time" timestyle="full" /><br
<fmt:settimezone value="est" />
value="<%=new date()%>" type="time" timestyle="full" /><br
timestyle="full" /><br />
date()%>" type="time" timestyle="full" /><br
上午09時25分12秒 est
上午09時25分12秒 est
此時區在該頁面内都有效
九:<fmt:timezone>
用來暫時設定時區.
<fmt:timezone
value="est">
type="time" timestyle="full" />
</fmt:timezone>
value="<%=new date()%>" type="time" timestyle="full"
<fmt:timezone value="est">
date()%>" type="time" timestyle="full" />
</fmt:timezone>
date()%>" type="time" timestyle="full"
/>
此标簽的時區隻是部分,在标簽開始至标簽結束内有效,其它地方無效,其它地方還是會使用預設時區
<fn:> function标簽 庫
functions 标簽庫中提供了一組常用的 el 函數,主要用于處理字元串,在 jsp 中可以直接使用這些函數。
在 jsp 檔案中使用 functions 标簽庫,要先通過 taglib 指令引入該标簽庫:
<%@taglib uri=”http://java.sun.com/jsp/jstl/functions” prefix=”fn” %.
fn:contains 函數用于判斷在源字元串中是否包含目标字元串,其文法為:
fn:contains(string
source,string target) -------boolean;
以上 source 參數指定源字元串, target 參數指定目标字元串,傳回類型為 boolean 。
例如對于以下 el 表達式:
${fn:contains(“tomcat”,”cat”)}
${fn:contains(“tomcat”,”cat”)}
第一個 el 表達式的值為 true ,第二個 el 表達式的值為 false 。
fn:containsignorecase 函數用于判斷在源字元串中是否包含目标字元串,并且在判斷時忽略大小寫,其文法為:
fn: containsignorecase (string
${fn:
containsignorecase (“tomcat”,”cat”)}
${fn: containsignorecase (“tomcat”,”mike”)}
fn:startswith 函數用于判斷源字元串是否以指定的目标字元串開頭,其文法為:
fn:startswith(string
source,string target) ----boolean
startswith (“tomcat”,”tom”)}
startswith (“tomcat”,”cat”)}
fn: endswith 函數用于判斷源字元串是否以指定的目标字元串結尾,其文法為:
fn:
endswith (string source,string target) ----boolean
endswith (“tomcat”,”cat”)}
endswith (“tomcat”,”tom”)}
fn:indexof 函數用于在源字元串中查找目标字元串,并傳回源字元串中最先與目标字元串比對的第一個字元的索引,如果在源字元串中不包含目标字元串,就傳回 -1 ,源字元串中的第一個字元的索引為 0 。 fn:indexof 函數的文法為:
indexof (string source,string target) ----int
以上 source 參數指定源字元串, target 參數指定目标字元串,傳回類型為 int 。
1 ${fn:
indexof (“tomcat”,”cat”)}<br/>
2 ${fn:
indexof (“2211221”,”21”)} <br/>
3 ${fn:
indexof (“tomcat”,”mike”)} <br/>
其輸出結果為:
1 3
2 1
3 -1
fn:replace 函數用于把源字元串中的一部分替換為另外的字元串,并傳回替換後的字元串。 fn:replace 函數的文法為:
replace (string source,string before,string after) ----string
以上 source 參數指定源字元串, before 參數指定源字元串中被替換的子字元串, after 參數指定用于替換的子字元串,傳回類型為 string 。
1 ${
fn: replace(“tomcat”,”cat”,”cat”)}<br/>
2 ${
fn: replace(“2008/1/9”,”/”,”-”)}<br/>
1 tomcat
2 2008-1-9
fn:substring 函數用于擷取源字元串中的特定子字元串,它的文法為:
fn:substring(string
source,int beginindex,int endindex) ------string
以上 source 參數指定源字元串, beginindex 參數表示子字元串中的第一個字元在源字元串中的索引,endindex 參數表示子字元串的最後一個字元在源字元串中的索引加 1 ,傳回類型為 string ,源字元串中的第一個字元的索引為 0 。
fn: substring (“tomcat”,0,3)}<br/>
fn: substring (“tomcat”,3,”6”)}<br/>
1 tom
2 cat
fn:substringbefore 函數用于擷取源字元串中指定子字元串之前的子字元串,其文法為:
fn:substringbefore(string
source,string target) ----string
以上 source 參數指定源字元串, target 參數指定子字元串,傳回類型為 string 。如果在源字元串中不包含特定子字元串,就傳回空字元串。
fn: substringbefore (“tomcat”,”cat”)}<br/>
fn: substringbefore (“mydata.txt”,”.txt”)}<br/>
2 mydata
fn:
substringafter 函數用于擷取源字元串中指定子字元串之後的子字元串,其文法為:
substringafter (string source,string target) ----string
fn: substringafter (“tomcat”,”tom”)}<br/>
fn: substringafter (“mydata.txt”,” mydata.”)}<br/>
1 cat
2 txt
fn:split 函數用于将源字元串拆分為一個字元串數組,其文法為:
split (string source,string delimiter) ----string[]
以上 source 參數指定源字元串, delimiter 參數指定用于拆分源字元串的分隔符,傳回類型為 string[] 。如果在源字元串中不包含 delimiter 參數指定的分隔符,或者 delimiter 參數為 null ,那麼在傳回的字元串數組中隻有一個元素,為源字元串。
<c:set
value=’${ fn: split (“www.mywebsite.org”,”.”)}’ var=”strs”/>
<c:foreach
var=”token” item=”${strs}”>
${token}<br/>
</c:foreach>
其輸出結果為:
www
mywebsite
org
再例如對于以下代碼:
<c:set value=’${ fn: split
(“www.mywebsite.org”,”-”)}’ var=”strs”/>
${strs[0]}
www.mywebsite.org
fn:join 函數用于将源字元串數組中的所有字元串連接配接為一個字元串,其文法為:
fn:join(string
source[],string separator) ----string
以上 source 參數指定源字元串數組, separator 參數指定用于連接配接源字元串數組中的各個字元串的分隔符,傳回類型為 string 。
例如對于以下代碼:
<%
string
strs[] = {“www”,”mywebsite”,”org”};
%>
<c:set value=”<%=strs%>” var=”strs”/>
${fn:join(strs,”.”)}
www.
mywebsite. org
fn:tolowercase 函數用于将源字元串中的所有字元改為小寫,其文法為:
fn:tolowercase(string
source) -----string
以上 source 參數指定源字元串,傳回類型為 string 。
fn:tolowercase(“tomcat”)
tomcat
touppercase 函數用于将源字元串中的所有字元改為大寫,其文法為:
touppercase (string source) -----string
touppercase (“tomcat”)
fn:trim 函數用于将源字元串中的開頭和末尾的空格删除,其文法為:
fn:trim(string
source) ----string
fn:trim(“ tomcat ”)
以上 el 表達式的值為“ tomcat ”。
fn:escapexml 函數用于将源字元串中的字元“ < ”、“ > ”、“ ” ”和“ & ”等轉換為轉義字元,本書第 1 章的 1.2 節( html 簡介)介紹了轉義字元的概念。 fn:escapexml 函數的行為與 <c:out> 标簽的 escapexml 屬性為 true 時的轉換行為相同, fn:escapexml 函數的文法為:
fn:escapexml(string source)
----string
例程 18-1 的 out.jsp 示範了 fn:escapexml 函數的用法。
<%@ page language="java" contenttype="text/html; charset=utf-8"
pageencoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions"
prefix="fn"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="content-type" content="text/html; charset=utf-8"
<title>out</title>
<body>
1.${fn:escapexml("<b> 表示粗體字 </b>")
}<br/>
2.<c:out value="<b> 表示粗體字 </b>"
escapexml="true"></c:out><br/>
3.${"<b> 表示粗體字 </b>"}<br/>
</html>
對于 out.jsp 中的以下代碼:
1.${fn:escapexml("<b> 表示粗體字 </b>")
1.&lt;b&gt; 表示粗體字 &lt;/b&gt;<br/>
2.&lt;b&gt; 表示粗體字 &lt;/b&gt;<br/>
3.<b> 表示粗體字 </b><br/>
fn:length 函數用于傳回字元串中的字元的個數,或者集合和數組的元素的個數,其文法為:
fn:length(source)
---- int
以上 source 參數可以為字元串、集合或者數組,傳回類型為 int 。
pageencoding="utf-8"%>
<%@page import="java.util.arraylist"%>
<title>length</title>
<%
int[] array = {1,2,3,4};
arraylist list = new arraylist();
list.add("one");
list.add("two");
list.add("three");
<c:set value="<%=array%>" var="array"></c:set>
<c:set value="<%=list%>" var="list"></c:set>
數組長度: ${fn:length(array)}<br/>
集合長度: ${fn:length(list)}<br/>
字元串長度: ${fn:length("tomcat")}<br/>
functions 标簽庫概覽
l fn:contains 函數 : 用于判斷在源字元串中是否包含目标字元串。
l fn:containsignorecase 函數 : 用于判斷在源字元串中是否包含目标字元串 , 并且在判斷時忽略大小寫。
l fn:startswith 函數 : 用于判斷源字元串是否以指定的目标字元串開頭。
l fn: endswith 函數:用于判斷源字元串是否以指定的目标字元串結尾。
l fn:indexof 函數:用于在源字元串中查找目标字元串,并傳回源字元串中最先與目标字元串比對的第一個字元的索引。
l fn:replace 函數:用于把源字元串中的一部分替換為另外的字元串,并傳回替換後的字元串。
l fn:substring 函數:用于擷取源字元串中的特定子字元串。
l fn:substringbefore 函數:用于擷取源字元串中指定子字元串之前的子字元串。
l fn: substringafter 函數:用于擷取源字元串中指定子字元串之後的子字元串
l fn:split 函數:用于将源字元串拆分為一個字元串數組。
l fn:join 函數:用于将源字元串數組中的所有字元串連接配接為一個字元串。
l fn:tolowercase 函數:用于将源字元串中的所有字元改為小寫。
l fn: touppercase 函數:用于将源字元串中的所有字元改為大寫。
l fn:trim 函數:用于将源字元串中的開頭和末尾的空格删除。
l fn:escapexml 函數:用于将源字元串中的字元“ < ”、“ > ”、“ ” ”和“ & ”等轉換為轉義字元。
l fn:length 函數:用于傳回字元串中的字元的個數,或者集合和數組的元素的個數
轉自 iteye 作者:elf8848
原文連結
</object>