zoukankan      html  css  js  c++  java
  • spring mvc出现 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endtime'

    转自:https://www.cnblogs.com/liaojie970/p/5566388.html

    在使用spring mvc中,绑定页面传递时间字符串数据给Date类型是出错:

    Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'expert.birthdate'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'birthdate': no matching editors or conversion strategy found 

    解决方法一:

    1.使对应Controller控制器继承 extends SimpleFormController 
    2.重写initBinder方法   

    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder){  
              SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");  
               dateFormat.setLenient(false);  
               binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));          
        }  

    注意SimpleDateFormat日期格式与页面日期格式要一致!

    解决方法二:

    Spring3.0以上的SimpleFormController 已经过时了,最新方式是使用@InitBinder注解的方式

    在对应的Controller控制器中

    复制代码
        @InitBinder
        protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            dateFormat.setLenient(false);
            binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
        }
    复制代码
  • 相关阅读:
    vs.net 2005 C# WinForm GroupBOX 的BUG?尝试读取或写入受保护的内存。这通常指示其他内存已损坏
    Git安装及基本使用
    c++实现将表达式转换为逆波兰表达式
    2015年倒数第6周学习报告
    读过的书及读后感
    c++实现队列
    链表插入排序(insertion-sort-list)
    test
    [转]maven入门
    几个学习Maven不错的网址
  • 原文地址:https://www.cnblogs.com/sharpest/p/8623683.html
Copyright © 2011-2022 走看看