我对 spring 和 thymeleaf 以及格式有疑问。 我已经注册了这样的转换服务:
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);
}
}
现在,只有当我将 thymeleaf 模板中的字段显式设置为字符串时,它才会格式化。
在这种情况下,不会发生格式化:
<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
仍然不知道为什么我的问题中的第二个选项有效,但这对我来说现在并不是那么重要。
关于java - Spring AnnotationFormatterFactory 与 Thymeleaf th :text, 仅在字段显式为字符串时格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37592823/