How to use custom Spring EL function in Thymeleaf?

Viewed 4231

I write a Function like this:

public interface SUtils {

    static String reverseString(String input) {
        StringBuilder backwards = new StringBuilder();
        for (int i = 0; i < input.length(); i++) {
            backwards.append(input.charAt(input.length() - 1 - i));
        }
        return backwards.toString();
    }
}

And register this function with StandardEvaluationContext.registerFunction. And in controller I use @Value("#{#reverseString('hello')}") can get the value. but in thymeleaf when I use ${reverseString('hello')} got an error Exception evaluating SpringEL expression: "reverseString('hello')".

How to use custom spel in thymeleaf?

3 Answers
Related