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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM