A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

EL表达式使用的函数所调用的底层方法是public static修饰的吗?

评分

参与人数 1技术分 +1 收起 理由
admin + 1

查看全部评分

2 个回复

倒序浏览
黑马网友  发表于 2011-7-22 12:57:00
沙发
在EL表达式中调用的只能是Java类的静态方法。
这个Java类的静态方法需要在TLD文件中描述,才可以被EL表达式调用。

评分

参与人数 1技术分 +1 收起 理由
admin + 1 相当正确,奖分了

查看全部评分

回复 使用道具 举报
您说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 编辑 ]

评分

参与人数 1技术分 +2 收起 理由
xiaolong + 2 给你个称号:贴码超人!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马