JSTL語法及參數
JSTL包含以下的標簽:
常用的標簽:如<c:out>、<c:remove>、<c:catch>、<c:set>等
條件標簽:如<c:if><c:when>、<c:choose>、<c:otherwise>等
URL標簽:如<c:import>、<c:redirect>和<c:url>等
XML標簽:如<xml:out>等
國際化輸出標簽:如<fmt:timeZone>等
SQL標簽:如<sql:query>、<sql:update>、<sql:transaction>等

一般用途的標簽:
1.<c:out>
沒有Body時的語法
<c:out value=”value” [escapeXml=”{true|false}”] [default=”defaultValue”]/>
有Body時的語法
<c:out value=”value” [escapeXml=”{true|false}”]>
這里是Body部分
</c:out>

名字 類型 描述
value Object 將要輸出的表達式
escapeXml boolean 確定以下字符:<,>,&,’,”在字符串中是否被除數,默認為true
default Object 如果vaule計算后的結果是null,那么輸出這個默認值

2.<c:set>
這個標簽用于在某個范圍(page、request、session、application等)中使用某個名字設定特定的值,或者設定某個已經存在的javabean對象的屬性。他類似于<%request.setAttrbute(“name”,”value”);%>
語法1:使用value屬性設定一個特定范圍中的屬性。
<c:set value=”value” var=”varName” [scope=”{page|request|session|application}”]/>
語法2:使用value屬性設定一個特定范圍中的屬性,并帶有一個Body。
<c:set var=”varName” [scope=”{page|request|session|application}”]>
Body部分
</c:set>
語法3:設置某個特定對象的一個屬性。
<c:set value=”value” target=”target” property=”propertyName”/>
語法4:設置某個特定對象的一個屬性,并帶有一個Body。
<c:set target=”target” property=”propertyName”>
Body部分
</c:set>

名字 類型 描述
value Object 將要計算的表到式。
var String 用于表示value 值的屬性,如果要在其他標簽中使用,就是通過這 個var指定的值來進行的。它相當于在標簽定義了一個變量,并且這個變量只能在標簽中的一個。
scope String var的有效范圍,可以是page|request|session|application中的一個
target String 將要設置屬性的對象,它必須是javabean或則java.util.Map對象
property Object 待設定的Target對象中的屬性名字,比如在javabean中有個name屬性,提供了setUserId方法,那么這里填userId。

3.<c:remove>
<c:remove var=”varName” [scope=”{page|request|session|application}”]/>

4.<c:catch>
這個標簽相當于捕獲在它里邊的標簽拋出的異常對象
<c:catch [var=”varName”]> //var是異常的名字
內容
</c:catch>

條件標簽
1. <c:if>
語法1:無Body情況
<c:if test=”testCondition” var=”varName” [scope=”page|request|session|application”]/>
語法2:有Body的情況
<c:if test=”testCondition” var=”varName” [scope=”page|request|session|application”]>
Body內容
</c:if>

名字 類型 描述
test Boolean 表達式的條件,相當于if()中的條件判斷語句。
var String 表示這個語句的名字。
scope String var這個變量的作用范圍。

2.<c:choose>
語法:<c:choose>
Body內容(<c:when>和<c:otherwise>子標簽)
</c:choose>
注意:它的Body只能由以下元素組成:
1) 空格
2) 0或多個<c:when>子標簽,<c:when>必須在<c:otherwise>標簽之前出現.
3) 0個或多個<c:otherwise>子標簽。

3.<c:when>
代表的是<c:choose>的一個條件分支,只能在<c:choose>中使用
語法:<c:when test=”testCondition”> //test是boolean類型,用于判斷條件真假
Body語句
</c:when>

4.<c:otherwise>
代表的是<c:choose>中的最后選擇。必須在最后出現
<c:otherwise>
內容
</c:otherwise>

迭代標簽
1.<c:forEach>
語法1:在Collection中迭代
<c:forEach[var=”varName”] items=”collection” [varStatus=”varStatusName”]
[begin=”begin”] [end=”end”] [step=”step”]
Body內容
</c:foeEach>

語法2:迭代固定的次數.
<c:forEach [var=”varName”] [varStatus=”varStatusName”]
[begin=”begin”] [end=”end”] [step=”step”]
Body內容
</c:foeEach>

名字 類型 描述
var String 迭代的參數,它是標簽參數,在其他標簽中通過它來引用這個標簽中的內容。
Items Collection、ArrayList、 要迭代的items集合.
Iterator、Map、String、
Eunmeration等
VarStatus String 表示迭代的狀態,可以訪問迭代自身的信息
Begin int 表示開始迭代的位置。
End int 表示結束迭代的位置。
Step int 表示迭代移動的步長,默認為1。

URL相關的標簽
1.<c:import>
語法1:資源的內容使用String對象向外暴露
<c:import url=”url” [context=”context”]
[var=”varName”] [scope=”{page|request|session|application}”] [charEncoding=”charEncoding”]>
內容
</c:import>

語法2:資源的內容使用Reader對象向外暴露。
<c:import url=”url” [context=”context”]
varReader=”varReaderName” [charEncoding=”charEncoding”]>
內容
</c:import>
名字 類型 描述
url String 待導入資源的URL,可以是相對路徑和絕對路徑,并且可以導入其他主機資源
context String 當使用相對路徑訪問外部context資源時,context指定了這個資源的名字。
var String 參數的名字。
scope String var參數的作用范圍。
cahrEncoding String 輸入資源的字符編碼。
varReader String 這個參數的類型是Reader,用于讀取資源。

2.<c:redirct>
語法1:沒有Body的情況.
<c:redirect url=”value” [context=”context”]/>
語法2:有Body情況下,在Body中指定查詢的參數
<c:redirect url=”value” [context=”context”]>
<c:param name=”name” value=”value”/>
</c:redirect>

3.<c:url>
語法1:沒有Body
<c:url value=”value” [context=”context”] [var=”varName”] [scope=”{page|request|session+application}”]/>
語法2:有Body
<c:url value=”value” [context=”context”] [var=”varName”] [scope=”{page|request|session+application}”]>
<c:param name=”name” value=”value”/>
</c:url>

名字 類型 描述
value String URL值
context String 當使用相對路徑訪問外部context資源時,context指定了這個資源的名字
var String 標識這個URL標量。
Scope String 變量作用范圍。

SQL相關的標簽
1.<sql:setDataSource>
2.<sql:query>
3.<sql:update>
4.<transaction>
5.<param>

1、c:forTokens

類似于js及java中的split函數,將一個字符串用某個字符分割成數組,然后逐個處理。items是需要處理的原始字符串,delims是分隔符

<c:forTokens var="item" items="www.trs.com.cn" delims=".">
${item }<br/>
</c:forTokens>

2、fmt.tld中比較有用的fmt:parseDate, fmt:formatDate, fmt:parseNumber, fmt:formatNumber,用來格式化時間與數字,例子如下:

<fmt:formatDate value="${item.createdTime }" pattern="yyyy-MM-dd HH:mm" />

3、fn.tld,jstl的函數,其中有

  • fn:contains 判斷一個字符串是否含有另外一個字符串,eg <c:if test="${fn:contains(str, searchString)}">;
  • fn:containsIgnoreCase 同上面的tag,只是不區分大小寫;
  • fn:endsWith 判斷一個字符串是否以某個字串結束,eg <c:if test="${fn:endsWith(filename, ".txt")}">
  • fn:escapeXml 將字符串轉換為沒有xml無法識別字符的字符串,eg ${fn:escapeXml(info)}
  • fn:indexOf 判斷某個字符在一個字符串中的位置,eg ${fn:indexOf(name, "-")}
  • fn:join 同jsjoin,將數組用字符拼接,eg ${fn:join(array, ";")}
  • fn:length 計算字符串的長度
  • fn:replace 替換某個字串, eg ${fn:replace(text, "-", "•")}
  • fn:split join的反過程,eg ${fn:split(customerNames, ";")}
  • fn:startsWith 是否以某個字串開始, eg <c:if test="${fn:startsWith(product.id, "100-")}">
  • fn:substring 獲取字串,eg ${fn:substring(zip, 6, -1)}
  • fn:substringAfter 從某個字串開始取字串,eg ${fn:substringAfter(zip, "-")}
  • fn:substringBefore 上一條的反過程
  • fn:toUpperCase 把字符串中的字符全部轉為大寫
  • fn:trim 去掉字符串兩邊的空格,eg ${fn.trim(name)}