简体   繁体   中英

Struts2 date converter not being used

I'm trying to do the simplest date conversion with a custom converter using struts2.

I have my custom date converter in place:

    public class DateConverter extends StrutsTypeConverter {

    public Object convertFromString(Map context, String[] values, Class toClass) {
        if (values != null && values.length > 0 && values[0] != null && values[0].length() > 0) {
            try {
                return SystemDate.convertStringToDate(values[0]);
            }
            catch(Exception e) {
                throw new TypeConversionException(e);
            }
        }
        return null;
    }

    public String convertToString(Map context, Object o) {
        if (o instanceof Date) {
            return SystemDate.convertDateToString((Date)o, false);
        }
        return "";
    }
}

In my global xwork-conversion I have set it up so that all dates are converted using my custom converter:

java.util.Date=com.example.converter.DateConverter

However, it is never getting called. When I try to submit a date through a textfield:

<s:textfield name="someDateField" />

Struts automatically converts it to a java.util.Date, without calling my converter.

I have other converters in place for my domain classes that are working as expected. However, for Dates, struts is making the conversion automatically instead of calling my converter.

Am I missing some setting?

I read that struts1 did not allow for custom date conversion, but it seems to be a problem already solved in struts2.

I found my problem, which is very specific, but I'm posting the solution here because it may help other people.

I was importing another internal project as a maven dependency, and this project defined the Date converter. For some reason that I could not understand, the imported project configuration was overriding the main project configuration.

So the problem was that the imported project xwork-conversion.properties was overriding the main project xwork-conversion.properties .

I tried to make the main project override the imported project's configuration, but with no avail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM