spring mvc使用过程中关于页面form绑定java.util.Date遇到的问题
2013-05-15 19:35
513 查看
最近在使用spring3.1的bind时候,遇到一个很蛋疼的错误,折腾了好久,记录与此,用于提醒自己.
页面form的时候date提交到controller的时候报错如下:
[com.dmx.controller.exception.resolver.SpringMvcHandlerExceptionResolver]org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'present' on field 'createTimeEnd': rejected value []; codes [typeMismatch.present.createTimeEnd,typeMismatch.createTimeEnd,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [present.createTimeEnd,createTimeEnd]; arguments []; default message [createTimeEnd]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createTimeEnd'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'createTimeEnd': no matching editors or conversion strategy found]
Field error in object 'present' on field 'createTimeStart': rejected value []; codes [typeMismatch.present.createTimeStart,typeMismatch.createTimeStart,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [present.createTimeStart,createTimeStart]; arguments []; default message [createTimeStart]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createTimeStart'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'createTimeStart': no matching editors or conversion strategy found]
查了很多资料,最后终于定位到问题:
重写controller里的initBinder方法,如下:
@InitBinder
public void initBinder(ServletRequestDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
// binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
}
页面form的时候date提交到controller的时候报错如下:
[com.dmx.controller.exception.resolver.SpringMvcHandlerExceptionResolver]org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'present' on field 'createTimeEnd': rejected value []; codes [typeMismatch.present.createTimeEnd,typeMismatch.createTimeEnd,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [present.createTimeEnd,createTimeEnd]; arguments []; default message [createTimeEnd]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createTimeEnd'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'createTimeEnd': no matching editors or conversion strategy found]
Field error in object 'present' on field 'createTimeStart': rejected value []; codes [typeMismatch.present.createTimeStart,typeMismatch.createTimeStart,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [present.createTimeStart,createTimeStart]; arguments []; default message [createTimeStart]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createTimeStart'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'createTimeStart': no matching editors or conversion strategy found]
查了很多资料,最后终于定位到问题:
重写controller里的initBinder方法,如下:
@InitBinder
public void initBinder(ServletRequestDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
// binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
}
相关文章推荐
- spring mvc使用过程中关于spring:bind和绑定java.util.Date遇到的问题
- 关于使用ant-design设计可动态添加FormItem的过程中遇到的问题记录。
- 关于easyui中datagrid在使用过程中遇到的问题
- 关于ActivityGroup使用过程中遇到的一点问题
- 对象中含有java.util.Date时使用JSONArray.fromObject时时间显示问题
- 使用idea的时候遇到的n个问题:第一个是关于注册过期,怎么才可以永久使用,第二个是关于maven绑定到idea,问题三:使用了@Slf4j注解后,log还是报错(这个需要安装一个插件)。
- 学习安卓开发过程中遇到关于R.Java文件的问题
- 关于handsontable的使用过程中遇到的问题
- 关于java.util.Date的使用
- 使用Java8过程中遇到的问题
- 关于我使用spring mvc框架做文件上传时遇到的问题
- 关于使用java Optional遇到的一些问题
- 关于在jsp页面插入java代码做字符串比较遇到的问题
- 关于在使用codeblocks+wxWidgets的过程中遇到的问题及解决
- 关于 weChat 页面开发过程中遇到的问题总结
- 我在使用Unity 发布IOS真机过程时遇到的关于JIT的问题列表
- 关于dwz框架查找带回使用过程中遇到的问题
- SpringMVC 参数绑定获取的CST时间(Java.util.Date)类型问题
- 关于使用百度地图过程中遇到的一些问题(一)
- 关于《自己动手写CPU》使用GNU工具过程遇到的问题