Binding Date Property to SpringMVC’s @ModelAttribute

Let say i have this java bean,

public class MyBean implements Serializable {
    private Integer id;
    private String userid;       
    private Date contacttime;
	
	// other setter and getter	

And i have a SpringMVC Controller like this, using @ModelAttribute as the method’s parameter,

@RequestMapping(value = "/doSomething", method = RequestMethod.POST)
public @ResponseBody Object doSomething(@ModelAttribute MyBean myBean) {
	// do something
}

Im sending HTTP Post which format is like this

id=2&
userid=3abc&
contacttime=130314101010

But it shows error on my server side, which looks like this,

Failed to convert from type java.lang.String to type java.util.Date for value '130314101010'; 
nested exception is java.lang.IllegalArgumentException

Usually, i have to manually use SimpleDateFormat to parse my “ddMMyyHHmmss” String into Date, but SpringMVC provide a very elegant way of handling with this kind of conversion. Just by using @DateTimeFormat

import org.springframework.format.annotation.DateTimeFormat;

public class MyBean implements Serializable {
    private Integer id;
    private String userid;       
	
	@DateTimeFormat(pattern = "ddMMyyHHmmss")
    private Date contacttime;
	
	// other setter and getter	

Dont forget to include JodaTime on your pom.xml

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

If not, you will find this kind of error,

org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.format.annotation.DateTimeFormat java.util.Date for value '130314101010'; nested exception is java.lang.IllegalStateException: JodaTime library not available - @DateTimeFormat not supported

Have fun with SpringMVC :D

Google+

7 Comments

Eduard Gresak

about 3 years ago

Nice and concise post, Edwin. This is what I already knew from the Spring mvc documentation and I am glad you confirmed it. And yet I keep getting this error: Field error in object 'filter' on field 'expiredDateFrom': rejected value [12.04.2014 11:20]; codes [typeMismatch.filter.expiredDateFrom,typeMismatch.expiredDateFrom,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [filter.expiredDateFrom,expiredDateFrom]; arguments []; default message [expiredDateFrom]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'expiredDateFrom'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java language=".lang.String"][/java] to required type [java language=".util.Date"][/java] for property 'expiredDateFrom': no matching editors or conversion strategy found] It seems the formatting annotations are completely ignored, I tried several datetime formats, nothing ever worked. My setting is: mvc portlet in Liferay, spring framework 4.0.2 release, Tomcat. I have the required spring configuration entries including: - - and I have joda library in classpath (through pom file) The Date field annotation for the field with the above error: @DateTimeFormat(pattern="dd.MM.yyyy HH:mm") Do you have any idea what else I should check?

Reply

edwin

about 3 years ago

hi Eduard Gresak, "no matching editors or conversion strategy found" means that SpringMVC couldnt read your annotation. Perhaps you missed something? I cannot tell since i couldnt see your complete sourcecode. Sorry i cant do much help :(

Himadri

about 3 years ago

You saved my day! Thank you man!

Reply

edwin

about 3 years ago

glad it could help :D

vikas tadge

about 3 years ago

its very useful. thanks edwin.

Reply

vivek sonoi

about 2 years ago

JodaDate time will not work with spring data jpa.

Reply

edwin

about 2 years ago

hi Vivek, thank you for your information :)

Leave a Comment

Please be polite. We appreciate that.
Your email address will not be published and required fields are marked


:-[ (B) (^) (P) (@) (O) (D) :-S ;-( (C) (&) :-$ (E) (~) (K) (I) (L) (8) :-O (T) (G) (F) :-( (H) :-) (*) :-D (N) (Y) :-P (U) (W) ;-)