標籤:
在之前的JSTL的總結中已經對函數標籤庫進行了一些說明,在這裡我再一次重新整理一下!
內建函數標籤庫介紹
引入該標籤庫的方法為:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
| 函數名 |
函數說明 |
使用舉例 |
fn:contains |
判斷字串是否包含另外一個字串 |
<c:if test="${fn:contains(name, searchString)}"> |
fn:containsIgnoreCase |
判斷字串是否包含另外一個字串(大小寫無關) |
<c:if test="${fn:containsIgnoreCase(name, searchString)}"> |
fn:endsWith |
判斷字串是否以另外字串結束 |
<c:if test="${fn:endsWith(filename, ".txt")}"> |
fn:escapeXml |
把一些字元轉成XML表示,例如 <字元應該轉為< |
${fn:escapeXml(param:info)} |
fn:indexOf |
子字串在母字串中出現的位置 |
${fn:indexOf(name, "-")} |
fn:join |
將數組中的資料聯合成一個新字串,並使用指定字元格開 |
${fn:join(array, ";")} |
fn:length |
擷取字串的長度,或者數組的大小 |
${fn:length(shoppingCart.products)} |
fn:replace |
替換 字串中指定的字元 |
${fn:replace(text, "-", "•")} |
fn:split |
把字串按照指定字元切分 |
${fn:split(customerNames, ";")} |
fn:startsWith |
判斷字串是否以某個子串開始 |
<c:if test="${fn:startsWith(product.id, "100-")}"> |
fn:substring |
截取子串 |
${fn:substring(zip, 6, -1)} |
fn:substringAfter |
擷取從某個字元所在位置開始的子串 |
${fn:substringAfter(zip, "-")} |
fn:substringBefore |
擷取從開始到某個字元所在位置的子串 |
${fn:substringBefore(zip, "-")} |
fn:toLowerCase |
轉為小寫 |
${fn.toLowerCase(product.name)} |
fn:toUpperCase |
轉為大寫 |
${fn.UpperCase(product.name)} |
fn:trim |
去除字串前後的空格 |
${fn.trim(name)} |
解釋
- fn:contains(string, substring) 如果參數string中包含參數substring,返回true
- fn:containsIgnoreCase(string, substring) 如果參數string中包含參數substring(忽略大小寫),返回true
- fn:endsWith(string, suffix) 如果參數 string 以參數suffix結尾,返回true
- fn:escapeXml(string) 將有特殊意義的XML (和HTML)轉換為對應的XML character entity code,並返回
- fn:indexOf(string, substring) 返回參數substring在參數string中第一次出現的位置
- fn:join(array, separator) 將一個給定的數組array用給定的間隔符separator串在一起,組成一個新的字串並返回。
- fn:length(item) 返回參數item中包含元素的數量。參數Item類型是數組、collection或者String。如果是String類型,傳回值是String中的 字元數。
- fn:replace(string, before, after) 返回一個String對象。用參數after字串替換參數string中所有出現參數before字串的地方,並返回替換後的結果
- fn:split(string, separator) 返回一個數組,以參數separator 為分割符分割參數string,分割後的每一部分就是數組的一個元素
- fn:startsWith(string, prefix) 如果參數string以參數prefix開頭,返回true
- fn:substring(string, begin, end) 返回參數string部分字串, 從參數begin開始到參數end位置,包括end位置的字元
- fn:substringAfter(string, substring) 返回參數substring在參數string中後面的那一部分字串
- fn:substringBefore(string, substring) 返回參數substring在參數string中前面的那一部分字串
- fn:toLowerCase(string) 將參數string所有的字元變為小寫,並將其返回
- fn:toUpperCase(string) 將參數string所有的字元變為大寫,並將其返回
- fn:trim(string) 去除參數string 首尾的空格 ,並將其返回
自訂函數庫第一步 自訂類 和 方法(public +static)
package com.pangsir.ty;
publicclassTestFunction{
/**
*
* 自訂類和方法 ,方法必須是public + static
* @param name
* @return
*/
publicstaticString toTest(String name){
return"練習自訂函數, "+name;
}
}
註: toTest()必須是 public static 的。
第二步 編寫自訂 tld 檔案,將此自訂tld檔案放在WEB-INF或者WEB-INF的任意子目錄下
<?xml version="1.0" encoding="UTF-8"?>
<taglibxmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<!--該檔案主要是受這個的影響:http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd -->
<description>test own defined functions library</description>
<display-name>test functions</display-name>
<!-- 定義的版本 -->
<tlib-version>1.0</tlib-version>
<!--這個名字可以隨便取,盡量與檔案名稱相同,這樣我們知道檔案在哪兒 -->
<short-name>test</short-name>
<!-- 這個地址是隨便取得。到時候jsp頁面引入這個地址 -->
<uri>http://codemihong.com/functions</uri>
<!-- 定義函數 -->
<function>
<!-- 自訂函數名稱(隨意) -->
<name>testFunction</name>
<!-- 定義函數的類全名稱 -->
<function-class>com.pangsir.ty.TestFunction</function-class>
<!--java.lang.String:傳回值類型 toTest:類定義的函數名(形參的資料類型) -->
<function-signature>java.lang.String toTest(java.lang.String)</function-signature>
</function>
</taglib>
說明:
- 標籤
<description>、<description-name>、<tlib-version>、<short-name>、<uri>內容隨意,注意這裡設定的uri後面jsp中要引入。
<function> 標籤中的 <name>標籤在JSP中要作為函數名調用,這裡的名字可以和實際的函數名一致,也可以不一致,可認為是實際函數名的別名。
<function-class>標籤是 包名+類名 (自訂類)。
<function-signature>是自訂函數的說明,如果是封裝類型,需寫完整路徑;如果是基礎資料型別 (Elementary Data Type),則不需要。如下又一樣本所示:
<function>
<description>
Returns the index withing a string of the first occurrence of a specified substring.
</description>
<name>indexOf</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>int indexOf(java.lang.String, java.lang.String)</function-signature>
<example>
${fn:indexOf(name, "-")}
</example>
</function>
補充說明:註冊JSTL函數,若uri為/WEB-INF/xxx.tld,則無需再下面tomcat中註冊
<!-- 註冊JSTL函數 -->
<jsp-config>
<taglib>
<taglib-uri>http://codemihong.com/functions</taglib-uri>
<taglib-location>/WEB-INF/myfunctions.tld</taglib-location>
</taglib>
</jsp-config>
第三步 在要使用自訂函數庫的JSP頁面中引入
<%@ taglib prefix="hp" uri="http://codemihong.com/functions" %>
第四步 使用:${首碼 + 冒號 + 函數的別名 }
<body>
<h1>測試JSTL--自訂函數庫</h1>
<spanstyle="color:#006600;"></span> ${hp:testFunction("人總是需要向前看的") }
</body>
第五步 測試
[應用篇]第六篇 JSTL之自訂函數標籤庫