前台传参时间类型不匹配:type 'java.lang.String' to required type 'java.util.Date' for property 'createDate'

springMVC action接收参数:

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'projectStep' on field 'createDate': rejected value [2016-6-23]; codes [typeMismatch.projectStep.createDate,typeMismatch.createDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [projectStep.createDate,createDate]; arguments []; default message [createDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.persistence.Column java.util.Date for value '2016-6-23'; nested exception is java.lang.IllegalArgumentException]


解决办法:

在action中添加方法如下,初始化属性.

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }


你可能感兴趣的:(Java后台)