第一步,首先定义一个public 的类,在该类中定义方法,这个方法必须是静态的,例如:
package com.tinysoft.cn.test;
//自定义函数,定义一个类,这个类必须是public的
//类中提供静态方法,实现功能
public class MyFunction {
//必须是静态方法
public static String toUpperCase(String s){
//转化为大写
return s.toUpperCase();
}
}
<?xml version="1.0" encoding="utf-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<!-- myfun和uri提供给jsp调用时引用,没有实际意思-->
<short-name>myfun</short-name>
<uri>http://www.itheima.com/jsp/myfunctions</uri>
<function>
<!-- 注意函数名和上一步定义的静态方法要一致-->
<name>toUpperCase</name>
<!-- 上面方法所在类的全路径名-->
<function-class>com.tinysoft.cn.test.MyFunction</function-class>
<!-- 函数的签名,函数的参数和返回值为基本数据类型时,则不需要签名-->
<function-signature>java.lang.String toUpperCase(java.lang.String)</function-signature>
</function>
</taglib>
第三步,在jsp中调用这个定义的函数
//注意javaee也内置了很多EL函数<body> <%String s="abcdefghijklmnopqrstuvwxyz"; pageContext.setAttribute("s", "abddgfdgdgd"); %> ${myfun:toUpperCase(s)} </body>
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="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"> <description>JSTL 1.1 functions library</description> <display-name>JSTL functions</display-name> <tlib-version>1.1</tlib-version> <short-name>fn</short-name> <uri>http://java.sun.com/jsp/jstl/functions</uri> <function> <description> Tests if an input string contains the specified substring. </description> <name>contains</name> <function-class>org.apache.taglibs.standard.functions.Functions</function-class> <function-signature>boolean contains(java.lang.String, java.lang.String)</function-signature> <example> <c:if test="${fn:contains(name, searchString)}"> </example> </function> <function> <description> Tests if an input string contains the specified substring in a case insensitive way. </description> <name>containsIgnoreCase</name> <function-class>org.apache.taglibs.standard.functions.Functions</function-class> <function-signature>boolean containsIgnoreCase(java.lang.String, java.lang.String)</function-signature> <example> <c:if test="${fn:containsIgnoreCase(name, searchString)}"> </example> </function> <function> <description> Tests if an input string ends with the specified suffix. </description> <name>endsWith</name> <function-class>org.apache.taglibs.standard.functions.Functions</function-class> <function-signature>boolean endsWith(java.lang.String, java.lang.String)</function-signature> <example> <c:if test="${fn:endsWith(filename, ".txt")}"> </example> </function> <function> <description> Escapes characters that could be interpreted as XML markup. </description> <name>escapeXml</name> <function-class>org.apache.taglibs.standard.functions.Functions</function-class> <function-signature>java.lang.String escapeXml(java.lang.String)</function-signature> <example> ${fn:escapeXml(param:info)} </example> </function> <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> <function> <description> Joins all elements of an array into a string. </description> <name>join</name> <function-class>org.apache.taglibs.standard.functions.Functions</function-class> <function-signature>java.lang.String join(java.lang.String[], java.lang.String)</function-signature> <example> ${fn:join(array, ";")} </example> </function> <function> <description> Returns the number of items in a collection, or the number of characters in a string. </description> <name>length</name> <function-class>org.apache.taglibs.standard.functions.Functions</function-class> <function-signature>int length(java.lang.Object)</function-signature> <example> You have ${fn:length(shoppingCart.products)} in your shopping cart. </example> </function> <function> <description> Returns a string resulting from replacing in an input string all occurrences of a "before" string into an "after" substring. </description> <name>replace</name> <function-class>org.apache.taglibs.standard.functions.Functions</function-class> <function-signature>java.lang.String replace(java.lang.String, java.lang.String, java.lang.String)</function-signature> <example> ${fn:replace(text, "-", "•")} </example> </function> <function> <description> Splits a string into an array of substrings. </description> <name>split</name> <function-class>org.apache.taglibs.standard.functions.Functions</function-class> <function-signature>java.lang.String[] split(java.lang.String, java.lang.String)</function-signature> <example> ${fn:split(customerNames, ";")} </example> </function> <function> <description> Tests if an input string starts with the specified prefix. </description> <name>startsWith</name> <function-class>org.apache.taglibs.standard.functions.Functions</function-class> <function-signature>boolean startsWith(java.lang.String, java.lang.String)</function-signature> <example> <c:if test="${fn:startsWith(product.id, "100-")}"> </example> </function> <function> <description> Returns a subset of a string. </description> <name>substring</name> <function-class>org.apache.taglibs.standard.functions.Functions</function-class> <function-signature>java.lang.String substring(java.lang.String, int, int)</function-signature> <example> P.O. Box: ${fn:substring(zip, 6, -1)} </example> </function> <function> <description> Returns a subset of a string following a specific substring. </description> <name>substringAfter</name> <function-class>org.apache.taglibs.standard.functions.Functions</function-class> <function-signature>java.lang.String substringAfter(java.lang.String, java.lang.String)</function-signature> <example> P.O. Box: ${fn:substringAfter(zip, "-")} </example> </function> <function> <description> Returns a subset of a string before a specific substring. </description> <name>substringBefore</name> <function-class>org.apache.taglibs.standard.functions.Functions</function-class> <function-signature>java.lang.String substringBefore(java.lang.String, java.lang.String)</function-signature> <example> Zip (without P.O. Box): ${fn:substringBefore(zip, "-")} </example> </function> <function> <description> Converts all of the characters of a string to lower case. </description> <name>toLowerCase</name> <function-class>org.apache.taglibs.standard.functions.Functions</function-class> <function-signature>java.lang.String toLowerCase(java.lang.String)</function-signature> <example> Product name: ${fn.toLowerCase(product.name)} </example> </function> <function> <description> Converts all of the characters of a string to upper case. </description> <name>toUpperCase</name> <function-class>org.apache.taglibs.standard.functions.Functions</function-class> <function-signature>java.lang.String toUpperCase(java.lang.String)</function-signature> <example> Product name: ${fn.UpperCase(product.name)} </example> </function> <function> <description> Removes white spaces from both ends of a string. </description> <name>trim</name> <function-class>org.apache.taglibs.standard.functions.Functions</function-class> <function-signature>java.lang.String trim(java.lang.String)</function-signature> <example> Name: ${fn.trim(name)} </example> </function> </taglib>
引用这些EL函数,必须导入standard.jar这个包,这个包中封装了所有Javaee内置函数,如:
配置文件fn.tld,jsp中引用EL内置函数来自于Functions这个类里面的所有静态方法:如EL内置函数有:package org.apache.taglibs.standard.functions; import java.lang.reflect.Array; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.Map; import java.util.StringTokenizer; import javax.servlet.jsp.JspTagException; import org.apache.taglibs.standard.resources.Resources; import org.apache.taglibs.standard.tag.common.core.Util; public class Functions { public static String toUpperCase(String input) { return input.toUpperCase(); } public static String toLowerCase(String input) { return input.toLowerCase(); } public static int indexOf(String input, String substring) { if (input == null) { input = ""; } if (substring == null) { substring = ""; } return input.indexOf(substring); } public static boolean contains(String input, String substring) { return indexOf(input, substring) != -1; } public static boolean containsIgnoreCase(String input, String substring) { if (input == null) { input = ""; } if (substring == null) { substring = ""; } String inputUC = input.toUpperCase(); String substringUC = substring.toUpperCase(); return indexOf(inputUC, substringUC) != -1; } public static boolean startsWith(String input, String substring) { if (input == null) { input = ""; } if (substring == null) { substring = ""; } return input.startsWith(substring); } public static boolean endsWith(String input, String substring) { if (input == null) { input = ""; } if (substring == null) { substring = ""; } int index = input.indexOf(substring); if (index == -1) { return false; } if ((index == 0) && (substring.length() == 0)) { return true; } return index == input.length() - substring.length(); } public static String substring(String input, int beginIndex, int endIndex) { if (input == null) { input = ""; } if (beginIndex >= input.length()) { return ""; } if (beginIndex < 0) { beginIndex = 0; } if ((endIndex < 0) || (endIndex > input.length())) { endIndex = input.length(); } if (endIndex < beginIndex) { return ""; } return input.substring(beginIndex, endIndex); } public static String substringAfter(String input, String substring) { if (input == null) { input = ""; } if (input.length() == 0) { return ""; } if (substring == null) { substring = ""; } if (substring.length() == 0) { return input; } int index = input.indexOf(substring); if (index == -1) { return ""; } return input.substring(index + substring.length()); } public static String substringBefore(String input, String substring) { if (input == null) { input = ""; } if (input.length() == 0) { return ""; } if (substring == null) { substring = ""; } if (substring.length() == 0) { return ""; } int index = input.indexOf(substring); if (index == -1) { return ""; } return input.substring(0, index); } public static String escapeXml(String input) { if (input == null) { return ""; } return Util.escapeXml(input); } public static String trim(String input) { if (input == null) { return ""; } return input.trim(); } public static String replace(String input, String substringBefore, String substringAfter) { if (input == null) { input = ""; } if (input.length() == 0) { return ""; } if (substringBefore == null) { substringBefore = ""; } if (substringBefore.length() == 0) { return input; } StringBuffer buf = new StringBuffer(input.length()); int startIndex = 0; int index; while ((index = input.indexOf(substringBefore, startIndex)) != -1) { buf.append(input.substring(startIndex, index)).append(substringAfter); startIndex = index + substringBefore.length(); } return input.substring(startIndex); } public static String[] split(String input, String delimiters) { if (input == null) { input = ""; } if (input.length() == 0) { String[] array = new String[1]; array[0] = ""; return array; } if (delimiters == null) { delimiters = ""; } StringTokenizer tok = new StringTokenizer(input, delimiters); int count = tok.countTokens(); String[] array = new String[count]; int i = 0; while (tok.hasMoreTokens()) { array[(i++)] = tok.nextToken(); } return array; } public static int length(Object obj) throws JspTagException { if (obj == null) { return 0; } if ((obj instanceof String)) { return ((String)obj).length(); } if ((obj instanceof Collection)) { return ((Collection)obj).size(); } if ((obj instanceof Map)) { return ((Map)obj).size(); } int count = 0; if ((obj instanceof Iterator)) { Iterator iter = (Iterator)obj; count = 0; while (iter.hasNext()) { count++; iter.next(); } return count; } if ((obj instanceof Enumeration)) { Enumeration enum_ = (Enumeration)obj; count = 0; while (enum_.hasMoreElements()) { count++; enum_.nextElement(); } return count; } try { return Array.getLength(obj); } catch (IllegalArgumentException ex) { throw new JspTagException(Resources.getMessage("FOREACH_BAD_ITEMS")); } } public static String join(String[] array, String separator) { if (array == null) { return ""; } if (separator == null) { separator = ""; } StringBuffer buf = new StringBuffer(); for (int i = 0; i < array.length; i++) { buf.append(array[i]); if (i < array.length - 1) { buf.append(separator); } } return buf.toString(); } }