Skip to content

How to refer to a static method in Thymeleaf html in java?

I need to use the getMoneyIntoWords() method in the invoice html. But I am getting errors. Refer the code and errors below

// Added below method in MoneyUtil.java

 public String getMoneyIntoWords(String input) {

        MoneyConverters converter = MoneyConverters.ENGLISH_BANKING_MONEY_VALUE;

        String str = converter.asWords(new BigDecimal(input));

        return StringUtils.capitalize(str.split("£")[0]);

    }   

<td rowspan="2" colspan="2">
         <span th:text="${MoneyUtil.getMoneyIntoWords(invoice.totalAmountAfterTax)} +'only'" />
       </td>

Error:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method moneyIntoWords(java.lang.Integer) on null context object

Answer

You need special syntax to call a static method in Thymeleaf:

<span th:text="${T(com.company.app.money.MoneyUtil).getMoneyIntoWords(invoice.totalAmountAfterTax)} +'only'" />

You will need to use the full qualified name of the MoneyUtil class.