struts2不支持String到Calendar的转换,参考了一下xwork的源码,想自己写一个。嫌麻烦,还是直接转到java.util.Date吧。把xwork的源代码贴一下,以后写别的转换器时可以参考。

public class XWorkBasicConverter extends DefaultTypeConverter ...{
private static String MILLISECOND_FORMAT = ".SSS";
final private static SimpleDateFormat RFC3399_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
......
}必须继承自ognl.DefaultTypeConverter这个类;两个常量在做Date转换时会用到,就贴上来了。
完整的从String转换到Date的函数,支持java.sql.Time, java.sql.Timestamp, java.util.Date,简单更改一下即可支持Calendar。

private Object doConvertToDate(Map context, Object value, Class toType) ...{
Date result = null;

if (value instanceof String && value != null && ((String)value).length() > 0) ...{
String sa = (String) value;
Locale locale = getLocale(context);
DateFormat df = null;
if (java.sql.Time.class == toType) ...{
df = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
} else if (java.sql.Timestamp.class == toType) ...{
Date check = null;
SimpleDateFormat dtfmt = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.MEDIUM,
locale);
SimpleDateFormat fullfmt = new SimpleDateFormat(dtfmt.toPattern() + MILLISECOND_FORMAT,
locale);
SimpleDateFormat dfmt = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT,
locale);

SimpleDateFormat[] fmts = ...{fullfmt, dtfmt, dfmt};
for (int i = 0; i < fmts.length; i++) ...{
try ...{
check = fmts[i].parse(sa);
df = fmts[i];
if (check != null) ...{
break;
}
} catch (ParseException ignore) ...{
}
}
} else if(java.util.Date.class == toType) ...{
Date check = null;
SimpleDateFormat d1 = (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
SimpleDateFormat d2 = (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);
SimpleDateFormat d3 = (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
SimpleDateFormat[] dfs = ...{d1, d2, d3, RFC3399_FORMAT}; //added RFC 3339 date format (XW-473)
for (int i = 0; i < dfs.length; i++) ...{
try ...{
check = dfs[i].parse(sa);
df = dfs[i];
if (check != null) ...{
break;
}
}
catch (ParseException ignore) ...{
}
}
}
//final fallback for dates without time
if (df == null)...{
df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
}
try ...{
df.setLenient(false); // let's use strict parsing (XW-341)
result = df.parse(sa);
if (! (Date.class == toType)) ...{
try ...{
Constructor constructor = toType.getConstructor(new Class[]...{long.class});
return constructor.newInstance(new Object[]...{new Long(result.getTime())});
} catch (Exception e) ...{
throw new XWorkException("Couldn't create class " + toType + " using default (long) constructor", e);
}
}
} catch (ParseException e) ...{
throw new XWorkException("Could not parse date", e);
}
} else if (Date.class.isAssignableFrom(value.getClass())) ...{
result = (Date) value;
}
return result;
}
完成转换的函数:

public Object convertValue(Map context, Object o, Member member, String s, Object value, Class toType) ...{
Object result = null;

if (value == null || toType.isAssignableFrom(value.getClass())) ...{
// no need to convert at all, right?
return value;
}

if (toType == String.class) ...{
result = doConvertToString(context, value);
} else if (toType == boolean.class) ...{
result = doConvertToBoolean(value);
&n
本文介绍如何在Struts2框架中实现自定义的日期转换器,以支持从字符串类型转换为Java.util.Date及其子类。通过分析XWork源代码,详细解释了转换器的设计与实现过程。

被折叠的 条评论
为什么被折叠?



