您说JSTL里的函数标签库吧,嗯,用法和EL表达式很相似。
您自己亲自去看看源代码啊。
首先,将jstl-1.2.jar解压出来,然后找到“META-INF/fn.tld”。 然后想办法打开它。比如用IE浏览器或者EditPlus等工具。
接着,看看里面的代码:[code=xml]<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>[/code]每个<function>标签,对应一个函数。仔细观察,这些函数都是“org.apache.taglibs.standard.functions.Functions”类中的。
第三步,从刚才咱们解压出来的那堆文件中,找到“org.apache.taglibs.standard.functions.Functions”类。
第四步,去下载一个DJ Java Decompiler工具。这个工具可以反编译.class文件,并将其还原成.java文件。
第五步,安装。
第六步,双击那些你好奇的.class文件。是不是static的看看就知道了。
我看到的结果:[code=java]// Decompiled by DJ v3.5.5.77 Copyright 2003 Atanas Neshkov Date: 2011-7-22 14:52:57
// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version!
// Decompiler options: packimports(3)
// Source File Name: Functions.java
package org.apache.taglibs.standard.functions;
import java.lang.reflect.Array;
import java.util.*;
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 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;
else
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 "";
else
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 "";
else
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 "";
else
return input.substring(0, index);
}
public static String escapeXml(String input)
{
if(input == null)
return "";
else
return Util.escapeXml(input);
}
public static String trim(String input)
{
if(input == null)
return "";
else
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;
int index;
for(startIndex = 0; (index = input.indexOf(substringBefore, startIndex)) != -1; startIndex = index + substringBefore.length())
buf.append(input.substring(startIndex, index)).append(substringAfter);
return buf.append(input.substring(startIndex)).toString();
}
public static String[] split(String input, String delimiters)
{
if(input == null)
input = "";
String array[];
if(input.length() == 0)
{
array = new String[1];
array[0] = "";
return array;
}
if(delimiters == null)
delimiters = "";
StringTokenizer tok = new StringTokenizer(input, delimiters);
int count = tok.countTokens();
array = new String[count];
int i = 0;
while(tok.hasMoreTokens())
array[i++] = tok.nextToken();
return array;
}
public static int length(Object obj)
throws JspTagException
{
int count;
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();
count = 0;
if(obj instanceof Iterator)
{
Iterator iter = (Iterator)obj;
count = 0;
for(; iter.hasNext(); iter.next())
count++;
return count;
}
if(obj instanceof Enumeration)
{
Enumeration enum_ = (Enumeration)obj;
count = 0;
for(; enum_.hasMoreElements(); enum_.nextElement())
count++;
return count;
}
count = Array.getLength(obj);
return count;
IllegalArgumentException ex;
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);
if(i < array.length - 1)
buf.append(separator);
}
return buf.toString();
}
}[/code]最后,使用DJ Java Decompiler工具打开一个.class文件后,会产生一个.jad文件。把后缀名改成.java就可以了。
不管您是否需要再重做一遍上述步骤,我认为您都应该把这个工具保存起来。因为很有用啊。
[ 本帖最后由 cxy_zy 于 2011-07-22 15:19 编辑 ] |