【问题标题】:Use Different Locale for @NumberFormat in Spring在 Spring 中为 @NumberFormat 使用不同的语言环境
【发布时间】:2014-02-01 04:29:06
【问题描述】:

我正在处理 Spring 3 项目,我需要为土耳其货币格式化 Long 字段。应用程序的默认Locale 是en_US。我使用@NumberFormat注解如下:

@NumberFormat(style=NumberFormat.Style.CURRENCY)
public Long getBudgetLimit() {
    return budgetLimit; 
}

注释在英语语言环境中起作用并格式化表单字段。但是,我需要为此字段使用土耳其语语言环境。有没有办法设置单个字段或页面的区域设置与应用程序的区域设置不同?

编辑:我自己找到了答案。在控制器中使用自定义编辑器,如下所示:

NumberFormat numberFormat = NumberFormat.getNumberInstance(new Locale("tr","TR"));
dataBinder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class,numberFormat, true));

并且不要使用@NumberFormat 注释。出于同样的原因,它给了我一个错误。

【问题讨论】:

    标签: spring locale number-formatting


    【解决方案1】:

    有很多方法,例如,您可以将 AnnotationFormatterFactory<NumberFormat> 的自定义实现注册为 bean,这样您就可以手动处理格式设置。这是documentented in the Spring documentation section 6.6。文档中的示例甚至处理了调整@NumberFormat 行为的用例。

    【讨论】:

    • 谢谢,这是一个更全面、更集中(在整个应用程序中改变@NumberFormat 的行为)的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    相关资源
    最近更新 更多