解决:Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;

快来打我* 2021-09-26 06:04 366阅读 0赞

关注微信公众号“假装正经的程序员”,回复“日期转换”即可获取解决方案

发生这一错误的主要原因是Controller类中需要接收的是Date类型,但是在页面端传过来的是String类型,最终导致了这个错误。

这里提供两种解决方案,一种是局部转换,一种是全局转换。

  1. <form action="login.do" method="post">
  2. <input type="text" name="birthday" value="2017-07-12 22:04:00">
  3. <input type="submit" value="提交">
  4. </form>

Center

一.局部转换

  1. @Controller
  2. public class UserController{
  3. @RequestMapping(value="/login.do")
  4. public String login(String username,Date birthday){
  5. System.out.println("________");
  6. return "";
  7. }
  8. //只需要加上下面这段即可,注意不能忘记注解
  9. @InitBinder
  10. public void initBinder(WebDataBinder binder, WebRequest request) {
  11. //转换日期
  12. DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  13. binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
  14. }
  15. }

Center 1

二.全局转换

1.创建CustomDate类实现WebBindingInitializer

  1. import java.text.DateFormat;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import org.springframework.beans.propertyeditors.CustomDateEditor;
  5. import org.springframework.web.bind.WebDataBinder;
  6. import org.springframework.web.bind.support.WebBindingInitializer;
  7. import org.springframework.web.context.request.WebRequest;
  8. /**
  9. * @作者:JackHisen(GWD)
  10. * @项目名:shoppingmall
  11. * @时间:2017-7-12 下午10:39:10
  12. * @version 1.0
  13. */
  14. public class CustomDate implements WebBindingInitializer{
  15. @Override
  16. public void initBinder(WebDataBinder binder, WebRequest request) {
  17. // TODO Auto-generated method stub
  18. //转换日期
  19. DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  20. binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
  21. }
  22. }

2.在Spring-MVC.xml中配置日期转换

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
  10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  11. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  12. <context:component-scan base-package="com.gwd.shopping" use-default-filters="false">
  13. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  14. </context:component-scan>
  15. <!-- 日期转换 -->
  16. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  17. <property name="webBindingInitializer">
  18. <bean class="com.gwd.shopping.core.web.CustomDate"/>
  19. </property>
  20. </bean>
  21. <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  22. <property name="prefix" value="/WEB-INF/back_page/"/>
  23. <property name="suffix" value=".jsp"/>
  24. </bean>
  25. </beans>

发表评论

表情:
评论列表 (有 0 条评论,366人围观)

还没有评论,来说两句吧...

相关阅读