springmvc接收date類型參數

springmvc在表單提交接收date類型參數的時候會報錯:Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'actionDate'java

1、利用spring的DateTimeFormat註解spring

springmvc提交表單時正確的接收date類型參數,主要分如下3個步驟:mvc

一、在須要由string轉Date的字段上加上DateTimeFormat註解,代碼以下:ui

@DateTimeFormat(pattern="yyyy-MM-dd")
private Date actionDate;

二、添加joda-time的jar包spa

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.3</version>
</dependency>

三、在springmvc配置文件中添加註解映射的支持,代碼以下:code

<mvc:annotation-driven />

這種方法我認爲是最好的方法。orm

2、自定義Converterblog

3、在controller中使用initBinder註解string

相關文章
相關標籤/搜索