标签:
在之前的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)} |
解释
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>
是自定义函数的说明,如果是包装类型,需写完整路径;如果是基本数据类型,则不需要。如下又一示例所示:<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>
<%@ taglib prefix="hp" uri="http://codemihong.com/functions" %>
<body>
<h1>测试JSTL--自定义函数库</h1>
<spanstyle="color:#006600;"></span> ${hp:testFunction("人总是需要向前看的") }
</body>
标签:
原文地址:http://www.cnblogs.com/pangxiansheng/p/24a5852859f8b0aab1f6fee6d61f57c8.html