我正在开发一个 Spring 3 项目,我需要格式化土耳其货币的 Long
字段。应用程序的默认区域设置
是en_US
。我使用了 @NumberFormat
注释,如下所示:
@NumberFormat(style=NumberFormat.Style.CURRENCY)
public Long getBudgetLimit() {
return budgetLimit;
}
注释在英语语言环境中工作并格式化表单字段。但是我需要在该字段中使用土耳其语语言环境。有没有办法将单个字段或页面的区域设置设置为不同于应用程序的区域设置?
编辑:我自己找到了答案。在 Controller 中使用自定义编辑器,如下所示:
NumberFormat numberFormat = NumberFormat.getNumberInstance(new Locale("tr","TR"));
dataBinder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class,numberFormat, true));
并且不要使用@NumberFormat
注释。出于同样的原因,它给了我一个错误。
最佳答案
有很多方法,但您可以例如注册 AnnotationFormatterFactory<NumberFormat>
的自定义实现作为一个 bean,它允许您手动处理格式。这是documentented in the Spring documentation section 6.6 。文档中的示例甚至处理调整 @NumberFormat
的用例的行为。
关于spring - 在 Spring 中对 @NumberFormat 使用不同的区域设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21091086/