Well, I have been using the underlying DefaultTypeConverter for a while
across various versions without any problems.  The StrutsTypeConverter is
really just a convienence class to abstract you from the XWork converter,
but there is no reason why you can't just use the XWork converter.

Here are 2 examples.  I highly recommend them.

public class DateConverter extends DefaultTypeConverter {
    Log log = LogFactory.getLog(DateConverter.class);

    public Object convertValue(Map map, Object object, Class aClass) {
        /***********************************************************Set
Standard Format*/
        String[] parsePatterns = {"MM/dd/yy","MM/dd/yyyy", "dd-MMM-yyyy",
"MM.dd.yyyy"};
        FastDateFormat df = FastDateFormat.getInstance(parsePatterns[0]);
        if (aClass == Date.class) {
            /********************************************************Get
First Value in Parameters Array*/
            String source = ((String[]) object)[0];
            if(source.equals("")) return null;
            Date transfer;
            try {
                transfer = DateUtils.parseDate(source, parsePatterns);
                return transfer;
            } catch (ParseException e) {
                throw new RuntimeException("Cannot convert " + source + " to
calendar type");
            }
        } else if (aClass == String.class) {
            Date o = (Date) object;
            return df.format(o);
        }
        return null;
    }
}

or

@Component("employeeConverter")
public class EmployeeConverter extends DefaultTypeConverter {
    private EmployeeManager employeeManager;
    Log log = LogFactory.getLog(EmployeeConverter.class);


    @Override
    public Object convertValue(Object value, Class toType) {
        if(toType == Employee.class){
            String[] ids = (String[])value;
            Long id = Long.parseLong(ids[0]);
            log.debug("Using " + id + " as the id.");
            return employeeManager.get(id);
            
        }
        else if(toType == String.class){
            log.debug(value.getClass());
            if ((Employee)value != null){
                return ((Employee)value).getId().toString();
            }
            else return null;
        }
        return null;
    }

    @Autowired
    public void setEmployeeManager(EmployeeManager employeeManager) {
        this.employeeManager = employeeManager;
    }
}

60% of the time, they work all of the time. 

Here is the StrutsTypeConverter code.  Strange that your method does not get
called.  toClass.equals(String.class) and toType == String.class should be
the same thing right? Equals is not overriden in java.lang.Class.  I don't
see anything else offhand, but then again I don't use this class.
    public Object convertValue(Map context, Object o, Class toClass) {
        if (toClass.equals(String.class)) {
            return convertToString(context, o);
        } else if (o instanceof String[]) {
            return convertFromString(context, (String[]) o, toClass);
        } else if (o instanceof String) {
            return convertFromString(context, new String[]{(String) o},
toClass);
        } else {
            return performFallbackConversion(context, o, toClass);
        }
    }




Ryan Peterson-4 wrote:
> 
> Hi guys,
> 
> I'm working on converting from jsp strings to custom objects, and custom
> objects to string in the jsp.
> 
> I'm using StrutsTypeConverter, and the convertFromString method works
> great.
> I'm able to debug/log and the output is as expected.  Unfortunately the
> convertToString method is never called.  I have seen several references to
> this being a bug: https://issues.apache.org/struts/browse/WW-2367
> 
> Has anyone else run into this issue?  What kind of workaround did you wind
> up implementing, and would you recommend it?
> 
> Thank you for any assistence,
> 
> Ryan
> 
> 

-- 
View this message in context: 
http://www.nabble.com/S2%3A-Custom-StrutsTypeConverter-tp21691184p21699924.html
Sent from the Struts - User mailing list archive at Nabble.com.


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

Reply via email to