【问题标题】:JSTL Bug in function endsWith?函数中的 JSTL 错误 endsWith?
【发布时间】:2013-05-25 14:33:03
【问题描述】:
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<c:set var="some" value="abcdef"/>
${fn:endsWith(some, 'ef')}

返回真

<c:set var="some" value="abcdefef"/>
${fn:endsWith(some, 'ef')}

返回假

看起来函数endsWith 从字符串的开头而不是结尾检查字符串。 如果来自第二个参数的字符串不仅出现在第一个参数的末尾,则该函数返回 false。

【问题讨论】:

  • 你用什么服务器来测试它?也许其他服务器的实现没有问题?

标签: java jsp jstl ends-with


【解决方案1】:

是的,它们是jstl中的一个错误

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());
}

它使用indexof而不是字符串的endsWith

【讨论】:

  • 而不是${endsWith(input, substring)},这是我使用的相当冗长的替代方案${fn:substring(input, fn:length(input)-fn:length(substring), fn:length(input)) == substring}
  • 更好的是,编写一个自定义函数,将indexOf() 替换为lastIndexOf()
  • 是否存在/您是否参考了一些提到此错误的 bugtracker?
【解决方案2】:

尝试将 JSTL JAR 文件替换为 Apache 版本 http://tomcat.apache.org/taglibs/standard/。它为我解决了同样的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    相关资源
    最近更新 更多