4
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

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

returns true

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

returns false

It looks like the function endsWith checks the string from its begining instead of from its end. If the string from second argument appers in the first argument not only at the its end, then the function returns false.

LancerX
  • 1,211
  • 7
  • 23
  • 40

2 Answers2

3

yes, their is a bug in 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());
}

it is using indexof instead of endsWith of String

  • Instead of `${endsWith(input, substring)}`, here's the rather verbose alternative I am using `${fn:substring(input, fn:length(input)-fn:length(substring), fn:length(input)) == substring}` – bergie3000 Aug 25 '15 at 18:53
  • Better yet, write a custom function replacing `indexOf()` with `lastIndexOf()` – bergie3000 Aug 25 '15 at 20:31
  • Does exist/have you got a reference to some bugtracker mentioning this bug? – reallynice Mar 02 '20 at 11:47
0

Try replacing the JSTL JAR files with the Apache version instead, http://tomcat.apache.org/taglibs/standard/. It fixes the same problem for me.

Montri M
  • 1,716
  • 14
  • 12