springmvc在表單提交接收date類型參數的時候會報錯:Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'actionDate'
一、利用spring的DateTimeFormat注解
讓springmvc提交表單時正確的接收date類型參數,主要分以下3個步驟:
1、在需要由string轉Date的字段上加上DateTimeFormat注解,代碼如下:
@DateTimeFormat(pattern="yyyy-MM-dd") private Date actionDate;
2、添加joda-time的jar包
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
3、在springmvc配置文件中添加注解映射的支持,代碼如下:
<mvc:annotation-driven />
這種方法我認為是最好的方法。
二、自定義Converter
三、在controller中使用initBinder注解