Spring: @DateTimeFormat is ignored in nested objects

in my example, the @DateTimeFormat annotation @DateTimeFormat ignored when nesting objects:

 class Person { private Date birthdate; // other fields @DateTimeFormat(pattern="dd.MM.yyyy") public Date getBirthdate(){ return birthdate; } // Other getters/setters } 

And I have a class that inserts these objects.

 class PersonGroup { private Person person1; private Person person2; // other fields @Valid public Person getPerson1(){ return person1; } // also tried without @Valid-Annotation public Person getPerson2(){ return person2; } // Other getters/setters } 

I am adding an object of type PersonGroup to my model in @Controler -Method:

 model.addAttribute("myGroup", filledPersonGroup); 

In JSP, I use print nested variables:

 <form:form modelAttribute="myGroup" action="..." > <form:input path="person1.birthdate" > <form:input path="person2.birthdate" > ... </form:form > 

But, unfortunately, the date values ​​in the input fields are not formatted correctly (but the date is indicated in principle.). It works when I directly add an instance of the Person class to the model.

Can someone tell me how I can handle this? I want the date of my nested objects to be formatted correctly.

I am using Spring 4.1.1-RELEASE

+6
source share
1 answer

Did you try to annotate the field, not the method?

 class Person { @DateTimeFormat(pattern="dd.MM.yyyy") private Date birthdate; // other fields public Date getBirthdate(){ return birthdate; } // Other getters/setters } 
+1
source

Source: https://habr.com/ru/post/978952/


All Articles