Issue Details (XML | Word | Printable)

Key: XW-490
Type: Bug Bug
Status: Resolved Resolved
Resolution: Duplicate
Priority: Minor Minor
Assignee: Rainer Hermanns
Reporter: Nils Eckert
Votes: 2
Watchers: 6
Operations

If you were logged in you would be able to see more operations.
XWork

XWorkBasicConverter.doConvertToString(...) ignores Locale for instances of Number

Created: 12/Mar/07 10:06 AM   Updated: 09/Mar/09 11:10 AM
Component/s: Type Conversion
Affects Version/s: 2.0.1
Fix Version/s: 2.1.3

File Attachments: 1. Text File patch_xw490.txt (9 kB)

Environment: Windows XP, Tomcat 5.5, Struts 2.0.6
Issue Links:
Container
 
Related

Flags: Patch


 Description  « Hide
I'm using Struts 2.0.6.

For the Type-Conversion the XWorkConverter is used.

My Problem is, that String values of my JSP-Textfields are converted with the doConvertToNumber into Double-Values with the context locale and a NumberFormater.

But the other way round in the doConvertToString(Map, Object) function the locale is not used.

 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
Claus Ibsen added a comment - 24/Mar/07 07:10 AM
Nils. Can you elaborate?

Can you show the jsp code that converts your text fields wrongly? And what you want the result to be?

Rainer Hermanns added a comment - 01/Apr/07 06:27 AM
Nils,
we need more infos on this issue.
Do you have a simple testcase to reproduce the problem?

Claus Ibsen added a comment - 09/Apr/07 04:17 PM
Okay I think I got the idea of what is wrong.

I got the idea to it as Denmark uses a locale where fractions/decimals seperator is , (comma) and not as US a dot.

So Nils could have a situations
a )where he converts from String to Dobule
b) from Dobule back to String

And in a it uses his locale but does not in b.

So if in (a) the user enters 123,99 (assuming Denmark as locale) then the input as a string is converted to a dobule as 123,99 using Denmark locale.
If the dobule from (a) should be printed back on the screen as a string then we have situation (b). If it's done wrong it would output 123.99 (as US locale)
We should output it as 123,99 as Danish locale.

Claus Ibsen added a comment - 09/Apr/07 04:20 PM
A patch with:
- unit tests demonstrating the bug
- a proposed fix to this bug
- a minor fix in XWorkBasicConverter as SimpleDateFormat is not thread safe so we should not reuse static objects

The patch is against HEAD

Claus Ibsen added a comment - 13/Apr/07 07:41 AM
> a minor fix in XWorkBasicConverter as SimpleDateFormat is not thread safe so we should not reuse static objects
Will be fixed with XW-473

Rainer Hermanns added a comment - 18/Apr/07 12:47 PM
Claus, thanks a lot for the patch.
I applied it for branch_2.0 since I am going to release later today...

Please go ahead and apply it against SVN trunk.

cheers,
Rainer

Rainer Hermanns added a comment - 19/Apr/07 05:28 AM
Sorry Claus, needed to fix this myself to release the Jira changelog for 2.0.2
fixed in SVN as r1489

Claus Ibsen added a comment - 04/May/07 05:05 AM
This issue causes side effect. We will rollback this fix so XW-512 will be fixed.

Claus Ibsen added a comment - 04/May/07 05:21 AM
I set this to future as this requires close work with Struts2 to avoid side effects such as XW-512.

I think the Struts tags should supply information if the tag want locale dependent number conversion or not.

Don Brown added a comment - 20/May/07 12:05 AM
Personally, I think we should close this as won't fix. Number formatting shouldn't happen at this level, as Claus pointed out, it screws up a bunch of other clients of the code. Formatting should be in the presentation layer, either in the Action or in JSTL format tags.

Rainer Hermanns added a comment - 21/May/07 03:22 AM
+1 from me...
We had several issues with that in the past and should leave this to the "view" layer's responsibility.

Rainer Hermanns added a comment - 22/May/07 02:10 AM
Rescheduled for 2.1

Amaury Crickx added a comment - 27/Sep/07 08:27 AM
I totally agree formatting should happen in the presentation layer which is where decision about it can be made.

Now what about parsing ?
Now we are in a situation where parsing in xwork uses locale and formatting doesn't.
This leads to situations where a user input of "10,34" in French locale gets transformed into "10.34" after the first round trip and "1034.0" after the second round trip.

Now there is a solution to format the textfield in the current locale
see https://issues.apache.org/struts/browse/WW-2176

But there is none yet that I'm aware of for parsing the string params sent after user input without resorting to hand made parsing, loosing the transparent type conversion for Number types.

Sebastian added a comment - 04/Dec/07 03:57 AM
I totally agree with Amaury's last post.

Right now I use Struts 2.11 with XWork 2.04 and encounter this issue. I opened a corresponding issue in the Struts2 bug tracker:
https://issues.apache.org/struts/browse/WW-2349

Maybe XWork and Struts2 should work a bit together, right now you commented out the localization in XWorkBasicConverter and Struts2 is screwed.

Dennis Doubleday added a comment - 18/Dec/07 12:34 PM
Copied comment from WW-2349 since it is XWork-specific:

I'm not sure if this should be a separate bug or not, but I'd like to add the following information. There is also disparity between the treatment of properties that are of type Double and those that are primitive double. When your property is a primitive double, XWork doesn't even localize parsing. XWorkBasicConverter delegates the conversion up to DefaultTypeConverter, which just uses Double.parseDouble() to do the conversion.

Rainer Hermanns added a comment - 01/Apr/08 04:27 AM
rescheduled, major refactorings regarding these issues is in progress...

Rene Gielen added a comment - 09/Mar/09 06:02 AM
Just stumbled upon the l10n conversion problems for a client's project, feels like opening Pandora's box...

I basically agree with the idea that type conversion is subject to the presentation layer, but the design of our stack requires that we stay in correct notation. See a request sequence like the following:

<action name="foo" class="example.FooAction">
   <result type="redirectAction">
         <param name="actionName">bar</param>
         <param name="barDoubleProperty">${fooDoubleProperty}</param>
   </result>
</action>

<action name="bar" class="example.BarAction">
  ...
</action>

then the evaluated fooDoubleProperty must have a locale specific number format to be correctly evaluated for the bar action invocation, because request parameters are always treated locale specific when the request is dipatched, clearly before action invocation and presentation. Most importantly this means that we need the locale's correct decimal sign around if applicable.

I think the side effects of the provided first hand solution have two root causes:
- the usage of the grouping separator in XWorkBasicConverter.doConvertFromNumberToString() is evil and should be set to false
- there is a long lurking issue with I18nInterceptor dismissing browser locale, causing mixup of locale context determination (XW-679)

I'll go and fix XW-679 now, and we should think of reenabling the commented out code in XWorkBasicConverter with the change of

numFormat.setGroupingUsed(false);

in XWorkBasicConverter.doConvertFromNumberToString().

Thoughts?

Rene Gielen added a comment - 09/Mar/09 10:04 AM
as a follow-up to my last comment, there is a killer argument against re-enabling the doConvertFromNumberToString(): no bookmarkable url should contain localized data (by default, of course). If needed though, the correct solution for my previous example would be to do something like

<param name="barDoubleProperty">${getText('format.number',{fooDoubleProperty})}</param>

although it has the downside that it heavily relates on extending ActionSupport.

Nevertheless, together with XW-679 applying formats as described in
http://cwiki.apache.org/confluence/display/WW/Formatting+Dates+and+Numbers
should present a way to have consistent turnarounds in a localized environment.

Rene Gielen added a comment - 09/Mar/09 11:10 AM
Solution:
applying formats as described in
http://cwiki.apache.org/confluence/display/WW/Formatting+Dates+and+Numbers
together with XW-679 fixed enables for a full display / parse turnaround in a given locale environment, leaving the decision how to format the display part to the presentation layer.

Nevertheless, we might want to think of handier solutions for future development.