spring - 在 Spring 中对 @NumberFormat 使用不同的区域设置

标签 spring locale number-formatting

我正在开发一个 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/

相关文章:

java - Dockerfile testcontainers 无法连接到 Ryuk

java - 我有一个错误,但依赖项包含在 gradle root 和 gradle 模块配置中,为什么?

java - 我如何连接两个用户,以便他们可以使用 J2EE 网络技术(JSP、Spring 等)共享公共(public)对象(游戏实例)?

java - 间接 Hibernate/JPA 方法调用丢失事务

javascript - 使用 TimezoneOffset 计算用户的时间

java - 我如何获得货币的货币符号,因为它会出现在其本地语言环境之一中?

java - 如何在 Java 中设置千位分隔符?

java - 语言环境序列

java - JFormattedTextField 格式化百分比数字?

Flutter/Dart - 将 double 格式格式化为带有千位分隔符和小数点后数字的字符串