spring mvc出现 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endtime'

摘要:
在使用springmvc中,绑定页面传递时间字符串数据给Date类型是出错:Failedtoconvertpropertyvalueoftype[java.lang.String]torequiredtype[java.util.Date]forproperty'expert.birthdate';nestedexceptionisjava.lang.IllegalArgumentExceptio

在使用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, newCustomDateEditor(dateFormat, false));          
    }  

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

解决方法二:

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

在对应的Controller控制器中

@InitBinder
    protected voidinit(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

免责声明:文章转载自《spring mvc出现 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endtime'》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇mui 上传图片MySQL、DM 行转列及字段去重(Group_Concat())下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

plsql和tsql常用函数比对

http://www.jb51.net/list/list_154_1.htm 数学函数 1.绝对值 S:select abs(-1) value O:select abs(-1) value from dual 2.取整(大) S:select ceiling(-1.001) value O:select ceil(-1.001) value from...

sql server中部分函数功能详解

1.TOP 子句 TOP 子句用于规定要返回的记录的数目。 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的。 SQL Server 的语法: SELECT TOP number|percent column_name(s) FROM table_name 2.’%%’查询 我们希望从上面的 "Persons" 表中选取居住的城市以 "A" 或...

【转】sql 中把datetime转换成string 函数

http://blog.knowsky.com/186776.htm http://www.cnblogs.com/fanadu/archive/2009/01/05/1368825.html 0 Feb 22 2006 4:26PM  CONVERT(CHAR(19), CURRENT_TIMESTAMP, 0) 1  02/22/06  CON...

sqlserver 保留小数方法

1. 使用 Round() 函数,如 Round(@num,2)  参数 2 表示 保留两位有效数字。 2. 更好的方法是使用 Convert(decimal(18,2),@num) 实现转换,decimal(18,2) 指定要保留的有效数字。 最好使用第二种,比如:convert(decimal(18,1),单价) 其中1表示保存1位小数,如果是0则表示...

SQL SERVER性能分析死锁检测数据库阻塞语句<转>

工作中数据库经常出现内存,找了篇文章 参照CSDN,中国风(Roy)一篇死锁文章 阻塞:其中一个事务阻塞,其它事务等待对方释放它们的锁,同时会导致死锁问题。 整理人:中国风(Roy) 参照Roy_88的博客 http://blog.csdn.net/roy_88/archive/2008/07/21/2682044.aspx 日期:2008.07.20...

mysql字符串 转 int-double CAST与CONVERT 函数的用法

MySQL 的CAST()和CONVERT()函数可用来获取一个类型的值,并产生另一个类型的值。两者具体的语法如下: CAST(value as type); CONVERT(value, type); 就是CAST(xxx AS 类型), CONVERT(xxx,类型)。 Sql代码 mysql>SELECTCAST('3.35'ASsign...