Struts2的核心代码在于xwork,下面是xwork的源代码的下载地址
http://release.opensymphony.com/xwork/2.0.7/
今天工作的时候发生了这个错误,弄了一个下午,还是没有看源代码的原因啊。
ERROR - ParametersInterceptor - [setParameters]: Unexpected Exception caught setting 'd-49653-p' on 'class dgut.ke.actions.SubjectAction: Error setting expression 'tempdate' with value '[Ljava.lang.String;@d73256'
这个错误的原因是Struts2在自动将页面上的数据设置到Action类中时,将string类型转换成Date类型的时候,格式匹配发生了错误。下面来查看源代码:
因为
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 rfc3399 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat[] dfs = {d1, d2, d3, rfc3399}; //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;
}
我传进去的date是11-8-31 3:46:14.000。
而dtfmt = M/d/yy h:mm:ss a
fullfmt =M/d/yy h:mm:ss a.SSS
dfmt = M/d/yy
这样就匹配不了了,就会抛出错误。
总结: 这样有三种方法:一事将date改成和formte匹配的格式,二是改local,将它设置成其它语言格式,那样可能可以匹配。三是写自己的类型匹配器(参见http://polaris.blog.51cto.com/1146394/315403 )。应该早点去下载源代码来查看的,那样就不用弄那么长时间了