[應用篇]第六篇 JSTL之自訂函數標籤庫

來源:互聯網
上載者:User

標籤:

 

在之前的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表示,例如 <字元應該轉為&lt; ${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, "-", "&#149;")}
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)
  1. package com.pangsir.ty;
  2. publicclassTestFunction{
  3. /**
  4. *
  5. * 自訂類和方法 ,方法必須是public + static
  6. * @param name
  7. * @return
  8. */
  9. publicstaticString toTest(String name){
  10. return"練習自訂函數, "+name;
  11. }
  12. }

註: toTest()必須是 public static 的。

第二步 編寫自訂 tld 檔案,將此自訂tld檔案放在WEB-INF或者WEB-INF的任意子目錄下
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <taglibxmlns="http://java.sun.com/xml/ns/j2ee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  5. version="2.0">
  6. <!--該檔案主要是受這個的影響:http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd -->
  7. <description>test own defined functions library</description>
  8. <display-name>test functions</display-name>
  9. <!-- 定義的版本 -->
  10. <tlib-version>1.0</tlib-version>
  11. <!--這個名字可以隨便取,盡量與檔案名稱相同,這樣我們知道檔案在哪兒 -->
  12. <short-name>test</short-name>
  13. <!-- 這個地址是隨便取得。到時候jsp頁面引入這個地址 -->
  14. <uri>http://codemihong.com/functions</uri>
  15. <!-- 定義函數 -->
  16. <function>
  17. <!-- 自訂函數名稱(隨意) -->
  18. <name>testFunction</name>
  19. <!-- 定義函數的類全名稱 -->
  20. <function-class>com.pangsir.ty.TestFunction</function-class>
  21. <!--java.lang.String:傳回值類型 toTest:類定義的函數名(形參的資料類型) -->
  22. <function-signature>java.lang.String toTest(java.lang.String)</function-signature>
  23. </function>
  24. </taglib>

說明:

  1. 標籤<description>、<description-name>、<tlib-version>、<short-name>、<uri>內容隨意,注意這裡設定的uri後面jsp中要引入。
  2. <function> 標籤中的 <name>標籤在JSP中要作為函數名調用,這裡的名字可以和實際的函數名一致,也可以不一致,可認為是實際函數名的別名。
  3. <function-class>標籤是 包名+類名 (自訂類)。
  4. <function-signature>是自訂函數的說明,如果是封裝類型,需寫完整路徑;如果是基礎資料型別 (Elementary Data Type),則不需要。如下又一樣本所示:
  1. <function>
  2. <description>
  3. Returns the index withing a string of the first occurrence of a specified substring.
  4. </description>
  5. <name>indexOf</name>
  6. <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
  7. <function-signature>int indexOf(java.lang.String, java.lang.String)</function-signature>
  8. <example>
  9. ${fn:indexOf(name, "-")}
  10. </example>
  11. </function>

補充說明:註冊JSTL函數,若uri為/WEB-INF/xxx.tld,則無需再下面tomcat中註冊

  1. <!-- 註冊JSTL函數 -->
  2. <jsp-config>
  3. <taglib>
  4. <taglib-uri>http://codemihong.com/functions</taglib-uri>
  5. <taglib-location>/WEB-INF/myfunctions.tld</taglib-location>
  6. </taglib>
  7. </jsp-config>
第三步 在要使用自訂函數庫的JSP頁面中引入

<%@ taglib prefix="hp" uri="http://codemihong.com/functions" %>

第四步 使用:${首碼 + 冒號 + 函數的別名 }
  1. <body>
  2. <h1>測試JSTL--自訂函數庫</h1>
  3. <spanstyle="color:#006600;"></span> ${hp:testFunction("人總是需要向前看的") }
  4. </body>
第五步 測試

 

[應用篇]第六篇 JSTL之自訂函數標籤庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.