Issue
In the JSP:
<input type = "number"
name = "score"
min = "0"
max = "100"
maxlength = "3"
step = "0.01" />
In the Action:
@Getter @Setter private Float score;
When the value has decimal places (eg. 56,78
), in the Action I get 5678.0
.
This is due to the fact that Struts2 is not aware that in my country ,
is the decimal separator, not the thousands separator.
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 ?
Solution
When the value has decimal places (eg. 56,78), in the Action I get 5678.0
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.
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 keeps the current locale in the action context and stores it in the session to set the action context with the new request.
This is due to the fact that Struts2 is not aware that in my country , is the decimal separator, not the thousands separator.
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.
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 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.
Answered By - Roman C Answer Checked By - Cary Denson (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.