taivo
taivo

Reputation: 139

Spring AnnotationFormatterFactory with Thymeleaf th:text, only formats when field is explicitly string

I have an issue with spring and thymeleaf and formatting. I have registered a conversion service like this:

public class CurrencyConversionService implements AnnotationFormatterFactory<CurrencyField> {

    @Override
    public Set<Class<?>> getFieldTypes() {
        return new HashSet<>(asList(new Class<?>[]{BigDecimal.class}));
    }

    @Override
    public Printer<?> getPrinter(CurrencyField annotation, Class<?> fieldType) {
        return new Printer<BigDecimal>() {
            @Override
            public String print(BigDecimal object, Locale locale) {
                return formatCurrency(object, "€");
            }
        };
    }

    @Override
    public Parser<?> getParser(CurrencyField annotation, Class<?> fieldType) {
        return (text, locale) -> stringToBigDecimal(text);
    }
}

Now, it only formats when i explicitly set the the field in thymeleaf template to string.

In this case, formatting is not happening:

<span th:text="${incoming_invoice.amount}"></span>

In this case, field is formatted correctly:

<span th:text="${''+incoming_invoice.amount}"></span>

What could be the issue here?

Upvotes: 0

Views: 741

Answers (1)

taivo
taivo

Reputation: 139

Found the answer. Variable needs to be in double brackets.

<p th:text="${{val}}">...</p>

Reference: https://github.com/thymeleaf/thymeleaf/issues/223

Still don't know why the second option in my questions works, but this is not really that important for me right now.

Upvotes: 1

Related Questions