3

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 ?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Enrique San Martín
  • 2,202
  • 7
  • 30
  • 51

1 Answers1

3

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • so the question is now, how i can get in the Struts Action the number formatted with the correct locale(?) -- Thx for the explanaition of the problem, it's seem to be very common outside of Us :) -- Anyway the appserver (jboss6.3eap) has an vm arg with the locale of en_US, but my browser it's with spanish locales, so the problems seems in the client side :( – Enrique San Martín Apr 16 '16 at 15:04
  • it is already different question, the number which is set to the action should be converted to the property type. If it's `float`, then it should be converted to `float`. If you can't use requested locale, you should use parameter for `i18n` interceptor, that will update action context. It should be done before the value is set to the property. – Roman C Apr 16 '16 at 17:26
  • Ok, this help a lot, i'm thinking between change the locale of the input "numer" to en for default (with jquery) or use this: http://stackoverflow.com/questions/29428888/how-to-submit-locale-aware-values-in-struts-2 (create a resource in the global and format the number), thx for the explanaition, now i have to find some solutions to this :D – Enrique San Martín Apr 19 '16 at 12:36
  • It's defaulting to en, I'm not sure how jQuery could be involved. You should post the code that you have tried. Actually your question as it's written in the current state doesn't show that you have a problem. You have asked about how Struts2 uses a decimal separator to specific locale and got an answer. What else are you looking is unclear. Please clarify your specific problem or add additional details to highlight exactly what you need. – Roman C Apr 19 '16 at 14:27
  • ok, i mean use jquery to set the locale of the widget (maybe a spinner) to "en-EN" so the display is with dot separator – Enrique San Martín Apr 19 '16 at 22:48
  • Yep, jquery can do the trick: $( "#spinner" ).spinner({ step: 0.01, numberFormat: "n", culture: "en" }); if you like can be added to your answer :) – Enrique San Martín Apr 19 '16 at 23:49