异常:
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'autoAlbumStream' on field 'begin_time': rejected value [2016-01-18 11:39:36]; codes [typeMismatch.autoAlbumStream.begin_time,typeMismatch.begin_time,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [autoAlbumStream.begin_time,begin_time]; arguments []; default message [begin_time]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'begin_time'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.util.Date for value '2016-01-18 11:39:36'; nested exception is java.lang.IllegalArgumentException]
Field error in object 'autoAlbumStream' on field 'end_time': rejected value [2016-01-20 20:50:30]; codes [typeMismatch.autoAlbumStream.end_time,typeMismatch.end_time,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [autoAlbumStream.end_time,end_time]; arguments []; default message [end_time]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'end_time'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.util.Date for value '2016-01-20 20:50:30'; nested exception is java.lang.IllegalArgumentException]
说明:
很明显,实体设置的是Date类型,但是页面是以字符串的形式将日期字符串转换为实体字段导致的不匹配。
解决办法:
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
} 可以将次方法置于controller或者servlet中。
还有更好的办法欢迎回复----------