This should give you an idea. It's modified from existing code so I haven't tested it. I've decided to use a collection of NumberFormat's. You could use any string -> decimal conversion technique you like.

This can be used to an action by created a <actionname>-conversion.properties will in the same directory as the class, with an entry:
<propertyName>=example.DecimalConverter

where <propertyName> is the name of the property name that needs custom conversion and <actionName> is the short class name of your action

-----

package example;

import org.apache.struts2.util.StrutsTypeConverter;

import java.util.Map;
import java.util.Locale;
import java.text.*;

import com.opensymphony.xwork2.util.TypeConversionException;

/**
* Multiple locale decimal conversion. Only applies to a single value parameter.
*/
public class DecimalConverter extends StrutsTypeConverter {

private static final NumberFormat defaultFormat = DecimalFormat.getInstance();
   private static final NumberFormat[] acceptedNumberFormats = {
           defaultFormat,
DecimalFormat.getInstance(Locale.GERMANY) };
   /**
    * Converts one or more String values to the specified class.
    *
    * @param context the action context
* @param values the String values to be converted, such as those submitted from an HTML form
    * @param toClass the class to convert to
    * @return the converted object
    */
public Object convertFromString(Map context, String[] values, Class toClass) {
       Number result = null;
       if (values != null) {
           if (values.length != 1) {
               super.performFallbackConversion(context, values, toClass);
           }
if (values.length > 0) { for (NumberFormat format : acceptedNumberFormats) {
                   try {
                       result = format.parse(values[0]);
                   } catch (ParseException e) {
                       // swallow the exception
                   }
               }
if (result == null) { throw new TypeConversionException("Not a recognised number format: "+values[0]);
               }
           }
       }
       return result;
   }

   /**
    * Converts the specified object to a String.
    *
    * @param context the action context
    * @param o       the object to be converted
    * @return the converted String
    */
   public String convertToString(Map context, Object o) {
       if (o instanceof Date) {
           return defaultFormat.format(o);
       } else {
           return null;
       }
   }
}


Ned Collyer wrote:
Thanks for the reply.

I was hoping for some code.

It's obviously been fixed before somewhere, so why do we need to all
implement it again on a per case basis.

I'll go hack something together - if its worthy i'll contrib back here.

I'm a bit sick of struts and its "quirks" tbh.


Jeromy Evans - Blue Sky Minds wrote:
The default type converter uses the current locale.

As mentioned in the referenced thread, you can:
- create a custom type converter for that field that accepts a wider variety of number formats; or; - use a string for that field in your action and handle the conversion yourself

To turn off the automatic "input" result for a conversion or validation error you may remove the workflow interceptor from your action's interceptor stack. I don't think that's a good idea though.




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to