如何使用本地化的十进制分隔符将十进制值从JSP传递到Action? [英] How to pass a decimal value from JSP to Action with a Localized decimal separator?

查看:72
本文介绍了如何使用本地化的十进制分隔符将十进制值从JSP传递到Action?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JSP中:

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

在行动中:

@Getter @Setter private Float score; 

当该值具有小数位(例如56,78)时,在操作"中,我将得到5678.0.

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

这是由于Struts2不知道在我的国家/地区,,十进制分隔符,而不是数千分隔符.

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

是否可以指示Struts2使用特定Locale 的小数点分隔符,以便能够正确转换浮点数?

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 ?

推荐答案

当该值具有小数位(例如56,78)时,在操作"中,我得到5678.0

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

这是因为该值是使用默认语言环境转换的. AFAIK Struts2类型转换支持区域设置,您可以按照本地化指南为资源包中的每个区域设置特定的格式.

This is because the value is converted using default locale. AFAIK Struts2 type conversion is locale aware, and you can follow the localization guide to define specific formats for each locale in resource bundle.

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

You can start with defining formats for numbers in the resource bundles. Looks like the number format is the same for different locales. Specific locale contains its own decimal format symbols, so formatted output for numbers depends only on the current locale used by Struts when the value is formatted for output or type-converted back to the number.

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

Struts2 keeps the current locale in the action context and stores it in the session to set the action context with the new request.

这是因为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中的注册语言环境. Struts 2(版本2.3.28)中的更多信息仅接受注册的语言环境.浏览器隐式请求语言环境,该语言环境可能具有不同的语言环境,而不是OS默认语言环境,并且显式地通过request_locale参数(由i18n拦截器处理).请参见获取i18n拦截器在操作类中检测到的用户区域设置.

Struts is aware about locale in your country if it's registered locale in the JVM you run. More about it in Struts 2 (version 2.3.28) only accepts registered locales. 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.

是否可以指示Struts2使用特定Locale的小数点分隔符,以便它能够正确转换浮点数?

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,并根据此语言环境设置数字格式,然后再向用户显示数字或将其提交给操作.

Struts is using decimal format for formatting and converting numbers, which is locale aware. All you need is set the current locale to Struts and format numbers according this locale before you show numbers to the user or submit them to the action.

这篇关于如何使用本地化的十进制分隔符将十进制值从JSP传递到Action?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆