You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Pierre TEMPLIER <pi...@gmail.com> on 2012/06/18 14:35:08 UTC

Struts2 : formatting and entering doubles consistently through the whole application not depending on locale

In a localized application with 2 languages : French and English.
I want users to be able to enter (HTML <input>) doubles formatted the
same way wether they are using english or french locale.
i.e :
- english_user enter "445,000.00" for orderItem.price and this data is
correctly parsed to 445000.0d and put into the corresponding bean
property
- french_user enter "445,000.00" for orderItem.price and this is also
correctly parsed to 445000.0d and put into the corresponding bean
property

i'm using struts2 2.3.4 with defaultStack and jdk 1.5.0_22
i'm using s:textfield like suggested in the docs :
http://struts.apache.org/2.3.4/docs/formatting-dates-and-numbers.html

<s:textfield name="orderItem.price"
value="%{getText('format.number',{orderItem.price})}" />

with english locale it's ok, with french locale it's not.
when submitting the form with french locale i have errors : "Invalid
field value for field xxx"
and in the logs i get : [WARN ]
com.opensymphony.xwork2.ognl.OgnlValueStack  - Error setting
expression 'orderItem.price' with value '[Ljava.lang.String;@c98901'
(struts is trying to use a setter with a String parameter whereas it
should find the one with a Double parameter)

I've also tried using a Converter with no success (see code at the end)

Is there any (simple) way to achieve what i've explained here ?

Regards,
Pierre.

== Converter Code ==
public class DoubleConverter extends StrutsTypeConverter {

 private static ThreadLocal<DecimalFormat> decimalFormat = new
ThreadLocal<DecimalFormat>() {
  protected DecimalFormat initialValue() {
   final DecimalFormat df = (DecimalFormat)
DecimalFormat.getInstance(Locale.US);
   df.applyPattern("#,##0.00");
   return df;
  };
 };

 public Object convertFromString(Map context, String[] values, Class toClass) {
  String myString = values[0];
  if (myString == null || "".equals(myString)) {
   return 0d;
  }
  try {
   return decimalFormat.get().parse(myString);
  } catch (ParseException e) {
   throw new TypeConversionException(e);
  }
 }

 public String convertToString(Map context, Object o) {
  if (o == null) {
   return "0.00";
  }
  return decimalFormat.get().format(((Double) o).doubleValue());
 }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts2 : formatting and entering doubles consistently through the whole application not depending on locale

Posted by Łukasz Lenart <lu...@googlemail.com>.
2012/6/19 Pierre TEMPLIER <pi...@gmail.com>:
> I was able to register an application-wide DoubleConverter with
> primitive type double this way :
> == xwork-conversion.properties ==
> # syntax: <type> = <converterClassName>
> double = com.company.app.DoubleConverter

It isn't a hack, it's a normal way to do the things ;-)

> it works but it looks more like a hack than something i can rely on as
> it based on the bug Łukasz indicated
> (https://issues.apache.org/jira/browse/WW-3171). I might be surprised
> to see this no more working when this bug is fixed.

I'll work on that as soon I'll finish with FileManager and Confitura
conference ;-)


Regards
-- 
Łukasz
mobile +48 606 323 122 http://www.lenart.org.pl/
Warszawa JUG conference - Confitura http://confitura.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts2 : formatting and entering doubles consistently through the whole application not depending on locale

Posted by Pierre TEMPLIER <pi...@gmail.com>.
that's it, the parser expects localized input.
I'd like to be able to handle parsing by myself for Doubles (without
depending on the locale).

I was able to register an application-wide DoubleConverter with
primitive type double this way :
== xwork-conversion.properties ==
# syntax: <type> = <converterClassName>
double = com.company.app.DoubleConverter

it works but it looks more like a hack than something i can rely on as
it based on the bug Łukasz indicated
(https://issues.apache.org/jira/browse/WW-3171). I might be surprised
to see this no more working when this bug is fixed.

Pierre.

On Mon, Jun 18, 2012 at 5:28 PM, Chris Pratt <th...@gmail.com> wrote:
> I think the problem is that the French locale specifies that it should be
> written as  "445.000,00" not  "445,000.00".  The parser expects localized
> input.
>  (*Chris*)
>
> On Mon, Jun 18, 2012 at 7:06 AM, Łukasz Lenart <lukasz.lenart@googlemail.com
>> wrote:
>
>> I think the problem is related to primitive converter which doesn't
>> include Locale in conversion, take a look on that bug and related
>>
>> https://issues.apache.org/jira/browse/WW-3171
>>
>>
>> Regards
>> --
>> Łukasz
>> mobile +48 606 323 122 http://www.lenart.org.pl/
>> Warszawa JUG conference - Confitura http://confitura.pl/
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts2 : formatting and entering doubles consistently through the whole application not depending on locale

Posted by Pierre TEMPLIER <pi...@gmail.com>.
issue created : https://issues.apache.org/jira/browse/WW-3843

Pierre.

On Wed, Jun 20, 2012 at 3:32 PM, Łukasz Lenart
<lu...@googlemail.com> wrote:
> 2012/6/20 Pierre TEMPLIER <pi...@gmail.com>:
>> Am I missing something ?
>
> Nothing, my mistake :/ I forgot to define aliases, please raise an issue
>
>
> Thanks & regards
> --
> Łukasz
> mobile +48 606 323 122 http://www.lenart.org.pl/
> Warszawa JUG conference - Confitura http://confitura.pl/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts2 : formatting and entering doubles consistently through the whole application not depending on locale

Posted by Łukasz Lenart <lu...@googlemail.com>.
2012/6/20 Pierre TEMPLIER <pi...@gmail.com>:
> Am I missing something ?

Nothing, my mistake :/ I forgot to define aliases, please raise an issue


Thanks & regards
-- 
Łukasz
mobile +48 606 323 122 http://www.lenart.org.pl/
Warszawa JUG conference - Confitura http://confitura.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts2 : formatting and entering doubles consistently through the whole application not depending on locale

Posted by Pierre TEMPLIER <pi...@gmail.com>.
reading the docs I discovered the possibility to override default
typeconverters (cf:
http://struts.apache.org/2.3.4/xwork-core/apidocs/com/opensymphony/xwork2/conversion/impl/XWorkBasicConverter.html
)

== Quote from the docs
As from version 2.3.2 you can override default converters by
implementting TypeConverter interface and specifying beans in
struts.xml as follow: <bean
type="com.opensymphony.xwork2.conversion.TypeConverter"
name="collection" class="com.application.MyCollectionConverter"
scope="singleton"/>

I tried with that approach by creating a new TypeConverter (roughly
copied from com.opensymphony.xwork2.conversion.impl.NumberConverter)
adding the following line in struts.xml right after the <struts> tag
<bean type="com.opensymphony.xwork2.conversion.TypeConverter"
name="number" class="com.company.app.NumberConverter"
scope="singleton"/>

but the webapp failed to start with the following error.

Caused by: Bean type interface
com.opensymphony.xwork2.conversion.TypeConverter with the name number
has already been loaded by [unknown location] - bean -
file:/D:/dev/tomcat/webapps/blank/WEB-INF/classes/struts.xml:10:132
	at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:227)

Am I missing something ?

Pierre.

On Tue, Jun 19, 2012 at 9:18 PM, Dave Newton <da...@gmail.com> wrote:
> On Tue, Jun 19, 2012 at 1:02 PM, Chris Pratt <th...@gmail.com>wrote:
>
>> If you want to handle parsing yourself, make your action properties take
>> "String" parameters and do the parsing in the action.
>>
>
> Probably better to do  your own type converter, though, particularly if
> it's app-wide.
>
> Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts2 : formatting and entering doubles consistently through the whole application not depending on locale

Posted by Dave Newton <da...@gmail.com>.
On Tue, Jun 19, 2012 at 1:02 PM, Chris Pratt <th...@gmail.com>wrote:

> If you want to handle parsing yourself, make your action properties take
> "String" parameters and do the parsing in the action.
>

Probably better to do  your own type converter, though, particularly if
it's app-wide.

Dave

Re: Struts2 : formatting and entering doubles consistently through the whole application not depending on locale

Posted by Chris Pratt <th...@gmail.com>.
If you want to handle parsing yourself, make your action properties take
"String" parameters and do the parsing in the action.
  (*Chris*)

On Tue, Jun 19, 2012 at 7:55 AM, Pierre TEMPLIER
<pi...@gmail.com>wrote:

> Locale.FRENCH and Locale FRANCE should output something like 345 987,246
>
> i can confirm that with a slightly modified struts-blank app
> (defaultStack and a textfield mapped to a Double) i'm having a
> conversion error for Doubles too. It doesn't like the thousand
> separator (space) and it converts coma to point ( ',' to '.' )
>
> Pierre.
>
> On Tue, Jun 19, 2012 at 9:33 AM, J. Garcia <jo...@gmail.com> wrote:
> > According to the Java formatting api, the French locale for integers
> would
> > look something like this:
> >
> > 345 987,246
> >
> > http://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html
> >
> > As I have been working on integer l10n, I have tested this, and if I set
> > French locale and enter an integer like this: 345 987, I get a conversion
> > error.
> >
> > J.
> >
> > On Mon, Jun 18, 2012 at 5:28 PM, Chris Pratt <thechrispratt@gmail.com
> >wrote:
> >
> >> I think the problem is that the French locale specifies that it should
> be
> >> written as  "445.000,00" not  "445,000.00".  The parser expects
> localized
> >> input.
> >>   (*Chris*)
> >>
> >> On Mon, Jun 18, 2012 at 7:06 AM, Łukasz Lenart <
> >> lukasz.lenart@googlemail.com
> >> > wrote:
> >>
> >> > I think the problem is related to primitive converter which doesn't
> >> > include Locale in conversion, take a look on that bug and related
> >> >
> >> > https://issues.apache.org/jira/browse/WW-3171
> >> >
> >> >
> >> > Regards
> >> > --
> >> > Łukasz
> >> > mobile +48 606 323 122 http://www.lenart.org.pl/
> >> > Warszawa JUG conference - Confitura http://confitura.pl/
> >> >
> >> > ---------------------------------------------------------------------
> >> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> > For additional commands, e-mail: user-help@struts.apache.org
> >> >
> >> >
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: Struts2 : formatting and entering doubles consistently through the whole application not depending on locale

Posted by Pierre TEMPLIER <pi...@gmail.com>.
Locale.FRENCH and Locale FRANCE should output something like 345 987,246

i can confirm that with a slightly modified struts-blank app
(defaultStack and a textfield mapped to a Double) i'm having a
conversion error for Doubles too. It doesn't like the thousand
separator (space) and it converts coma to point ( ',' to '.' )

Pierre.

On Tue, Jun 19, 2012 at 9:33 AM, J. Garcia <jo...@gmail.com> wrote:
> According to the Java formatting api, the French locale for integers would
> look something like this:
>
> 345 987,246
>
> http://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html
>
> As I have been working on integer l10n, I have tested this, and if I set
> French locale and enter an integer like this: 345 987, I get a conversion
> error.
>
> J.
>
> On Mon, Jun 18, 2012 at 5:28 PM, Chris Pratt <th...@gmail.com>wrote:
>
>> I think the problem is that the French locale specifies that it should be
>> written as  "445.000,00" not  "445,000.00".  The parser expects localized
>> input.
>>   (*Chris*)
>>
>> On Mon, Jun 18, 2012 at 7:06 AM, Łukasz Lenart <
>> lukasz.lenart@googlemail.com
>> > wrote:
>>
>> > I think the problem is related to primitive converter which doesn't
>> > include Locale in conversion, take a look on that bug and related
>> >
>> > https://issues.apache.org/jira/browse/WW-3171
>> >
>> >
>> > Regards
>> > --
>> > Łukasz
>> > mobile +48 606 323 122 http://www.lenart.org.pl/
>> > Warszawa JUG conference - Confitura http://confitura.pl/
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> > For additional commands, e-mail: user-help@struts.apache.org
>> >
>> >
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts2 : formatting and entering doubles consistently through the whole application not depending on locale

Posted by "J. Garcia" <jo...@gmail.com>.
According to the Java formatting api, the French locale for integers would
look something like this:

345 987,246

http://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html

As I have been working on integer l10n, I have tested this, and if I set
French locale and enter an integer like this: 345 987, I get a conversion
error.

J.

On Mon, Jun 18, 2012 at 5:28 PM, Chris Pratt <th...@gmail.com>wrote:

> I think the problem is that the French locale specifies that it should be
> written as  "445.000,00" not  "445,000.00".  The parser expects localized
> input.
>   (*Chris*)
>
> On Mon, Jun 18, 2012 at 7:06 AM, Łukasz Lenart <
> lukasz.lenart@googlemail.com
> > wrote:
>
> > I think the problem is related to primitive converter which doesn't
> > include Locale in conversion, take a look on that bug and related
> >
> > https://issues.apache.org/jira/browse/WW-3171
> >
> >
> > Regards
> > --
> > Łukasz
> > mobile +48 606 323 122 http://www.lenart.org.pl/
> > Warszawa JUG conference - Confitura http://confitura.pl/
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>

Re: Struts2 : formatting and entering doubles consistently through the whole application not depending on locale

Posted by Chris Pratt <th...@gmail.com>.
I think the problem is that the French locale specifies that it should be
written as  "445.000,00" not  "445,000.00".  The parser expects localized
input.
  (*Chris*)

On Mon, Jun 18, 2012 at 7:06 AM, Łukasz Lenart <lukasz.lenart@googlemail.com
> wrote:

> I think the problem is related to primitive converter which doesn't
> include Locale in conversion, take a look on that bug and related
>
> https://issues.apache.org/jira/browse/WW-3171
>
>
> Regards
> --
> Łukasz
> mobile +48 606 323 122 http://www.lenart.org.pl/
> Warszawa JUG conference - Confitura http://confitura.pl/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: Struts2 : formatting and entering doubles consistently through the whole application not depending on locale

Posted by Łukasz Lenart <lu...@googlemail.com>.
I think the problem is related to primitive converter which doesn't
include Locale in conversion, take a look on that bug and related

https://issues.apache.org/jira/browse/WW-3171


Regards
-- 
Łukasz
mobile +48 606 323 122 http://www.lenart.org.pl/
Warszawa JUG conference - Confitura http://confitura.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org