[应用篇]第六篇 JSTL之自定义函数标签库

在之前的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>是自定义函数的说明,如果是包装类型,需写完整路径;如果是基本数据类型,则不需要。如下又一示例所示:
  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>

第五步 测试





posted on 2016-08-14 10:41  胖先生  阅读(552)  评论(0编辑  收藏  举报