springmvc接收date类型参数

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注解

原文地址:https://www.cnblogs.com/rookie-newbie/p/7993499.html