你的位置:首页 > 软件开发 > Java > springmvc关于前台日期作为实体类对象参数类型转换错误

springmvc关于前台日期作为实体类对象参数类型转换错误

发布时间:2017-06-25 00:01:07
页面报错: 后台错误:Field error in object user on field birthday: rejected value [2013-06-24]; codes [typeMismatch.user.birthday,typeMismatch.b ...

springmvc关于前台日期作为实体类对象参数类型转换错误

页面报错:

springmvc关于前台日期作为实体类对象参数类型转换错误

 

后台错误:

Field error in object 'user' on field 'birthday': rejected value [2013-06-24]; codes [typeMismatch.user.birthday,typeMismatch.birthday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.birthday,birthday]; arguments []; default message [birthday]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'birthday'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.util.Date for value '2013-06-24'; nested exception is java.lang.IllegalArgumentException]

 

解决方案1:在对应的实体类属性上加入   @DateTimeFormat(pattern = "yyyy-MM-dd")

springmvc关于前台日期作为实体类对象参数类型转换错误

 

解决方案2:不使用  <mvc:annotation-driven/>注解

使用  DefaultAnnotationHandlerMapping  和   AnnotationMethodHandlerAdapter  注解驱动配置 

在对应的实体类属性上加入   @DateTimeFormat(pattern = "yyyy-MM-dd")

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">      <property name="webBindingInitializer">        <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">          <property name="conversionService">            <bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/>          </property>        </bean>      </property>    </bean>

原标题:springmvc关于前台日期作为实体类对象参数类型转换错误

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录