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-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to