如何使用本地化小数点分隔符将小数值从 JSP 传递到 Action?

How to pass a decimal value from JSP to Action with a Localized decimal separator?

在JSP:

<input type = "number" 
       name = "score" 
        min = "0" 
        max = "100" 
  maxlength = "3"
       step = "0.01" />

在行动中:

@Getter @Setter private Float score; 

当值有小数位时(例如 56,78),在 Action 中我得到 5678.0.

这是因为 Struts2 不知道在我的国家 ,decimal 分隔符,而不是 分隔符.

有没有办法指示Struts2使用特定Locale的小数点分隔符,这样就可以将浮点数转换数字正确吗?

When the value has decimal places (eg. 56,78), in the Action I get 5678.0

这是因为值是使用默认区域设置转换的。 AFAIK Struts2 类型转换是区域设置感知的,您可以按照本地化指南为资源包中的每个区域设置定义特定格式。

资源包中的编号可以从 defining formats 开始。看起来数字格式对于不同的语言环境是相同的。特定语言环境包含其自己的十进制格式符号,因此数字的格式化输出仅取决于 Struts 在值格式化输出或类型转换回数字时使用的当前语言环境。

Struts2 将当前语言环境保留在操作上下文中并将其存储在会话中以使用新请求设置操作上下文。

This is due to the fact that Struts2 is not aware that in my country , is the decimal separator, not the thousands separator.

如果

Struts 在您 运行 的 JVM 中注册了区域设置,则它知道您所在国家/地区的区域设置。在 . The locale is implicitly requested by the browser, which might have different locale rather than OS default locale, and explicitely via request_locale parameter, which is handled by i18n interceptor. See Get user locale detected by i18n interceptor in action class.

中了解更多信息

Is there a way to instruct Struts2 to use the decimal separator of a specific Locale, so that it will be able to convert the floating numbers correctly?

Struts 使用十进制格式来格式化和转换数字,这是区域设置感知的。您只需将当前区域设置为 Struts 并根据此区域设置格式化数字,然后再向用户显示数字或将其提交给操作。