今天由于开发中采用volecity模板,没办法用jstl标签,导致日期一直转换出现400.查了很久才发现解决方案
原因 :spring3 mvc 的controller方法如果有参数会自动映射传入的来的parameter,但是一个date类型的值传进来会被认为是string类型,如果你需要映射至一个date字段,就会报类型不匹配的exception.如果是spring2.5即需通过extends simpleFormController override initBinder方法,注册一个自定义的CustomEditor.但spring3里,这个simpleformController被@Deprecated了,取而代之的是注解的方式即:
在control哪里加上这段代码
@InitBinder
protected void initBinder(ServletRequestDataBinder binder) throws Exception {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
CustomDateEditor editor = new CustomDateEditor(df, false);
binder.registerCustomEditor(Date.class, editor);
}
问题就解决了
具体的可以参考下面这篇文章
原文
查了很久终于解决了 干
修正:其实应该是这样说,spring他认的 时间标准格式是:yyyy-MM-dd HH:mm:ss,用别的格式都不行个人猜测,有时间去验证下。我用/ 这个的就不行