I'm unfamiliar with Jsp and Jstl , I created this little comparator to sort all my users by name:
<c:set var="userList" value="${fn:toList(users)}"/>
<c:set var="comparator" value="${new Comparator() {
public int compare(Object o1, Object o2) {
User u1 = (User) o1;
User u2 = (User) o2;
return u2.getName().compareTo(u1.getName());
}
}}" />
<c:set var="sortedUsers" value="${fn:sort(userList, comparator)}
importing the the fn taglib into my JSP file :
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
I keep getting the same error:
The function toList cannot be located with the specified prefix
[...]
The function sort cannot be located with the specified prefix
What am I doing wrong ? the import seems to me the correct one
Where exactly did you read/learn that this would work? I'm at least not seeing toList
nor sort
functions listed in the official documentation. So the errors you're getting are completely correct.
You need to solve the X of your XY-problem in a different way.
Either make sure that the model (the list behind ${users}
) is already in exactly the desired shape (i.e. already sorted) by the controller (e.g. servlet) before passing it to the view (the JSP). This is the most correct MVC approach.
Or, make use of the EL 3.0 stream functionality instead.
${users.stream().sorted((l, r) -> l.name.compareTo(r.name)).toList()}