0

i have a java web application using struts2 (vers: 2.3.15) and I need to apply type convertion to a int

I wrote the following code:

package sistema.conversores;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;

/**
 *
 * @author Administrador
 */
public class IntegerConverter extends StrutsTypeConverter {

    public Object convertFromString(Map context, String[] values, Class toClass) {
System.out.println("load");
        if (values.length != 1) {
            super.performFallbackConversion(context, values, toClass);
        }
System.out.println("conversion");
        final String numero = values[0];

        try {
            return Integer.parseInt(numero);
        } catch (Exception e) {
            return 0;
        }
    }

    public String convertToString(Map context, Object o) {
        final NumberFormat formatter = new DecimalFormat("#0");
System.out.println("lectura");
        if (o instanceof Integer) {
            return formatter.format(o);
        } else {
            return String.valueOf(o);
        }
    }
}

And append it to xwork-conversion.properties

# syntax: <type> = <converterClassName>
int = sistema.conversores.IntegerConverter
java.util.Date = sistema.conversores.DateConverter

But the int type conversion dont run, any other conversion run fine, Also the int type conversion work fine in struts2 vers 2.1.6

How can I fix this?

Aegis
  • 113
  • 7
  • 27
  • Why? S2 will already do int conversion. What specific issue are you having? – Dave Newton Jul 14 '13 at 18:47
  • i have a a field in the form that show "-" if other field is no set, when i send the post the "-" change to "0" in the type conversion. in struts 2.1.6 work fine but it dont work on 2.3.15 – Aegis Jul 14 '13 at 18:50
  • Do you have conversion errors? What they are? – Roman C Jul 14 '13 at 19:54
  • yes, the field i use can be "-" and that is't a number so i must change it to 0, but with the new version of struts2 it return to the form with the validator error without passing from the type conversion – Aegis Sep 05 '13 at 22:11

0 Answers0