Spring AnnotationFormatterFactory with Thymeleaf th:text,仅在字



我对弹簧和百里香叶以及格式有问题。我已经注册了这样的转换服务:

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);
    }
}

现在,它仅在我明确将百里香叶模板中的字段设置为字符串时格式化。

在这种情况下,不会发生格式设置:

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

在这种情况下,字段的格式正确:

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

这里可能有什么问题?

找到了答案。变量需要放在双括号中。

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

参考: https://github.com/thymeleaf/thymeleaf/issues/223

仍然不知道为什么我问题中的第二个选项有效,但这对我来说现在并不那么重要。

最新更新