Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: @DateTimeFormat ignored in nested Objects

in my example the @DateTimeFormat annotation is 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 which nests this 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 add an object of the type PersonGroup to my model in a @Controler-Method like this:

model.addAttribute("myGroup", filledPersonGroup);

In an JSP I use print the 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 shown in principle.). It works, when I directly add an instance of the class Person to the model.

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

I use Spring 4.1.1-RELEASE

like image 809
Tarator Avatar asked Dec 01 '14 17:12

Tarator


1 Answers

Have you tried annotating the field rather than the method?

class Person {
    @DateTimeFormat(pattern="dd.MM.yyyy")
    private Date birthdate;
     // other fields

    public Date getBirthdate(){
       return birthdate;
    }
    // Other getters/setters 
}
like image 121
Rikitikitaco Avatar answered Oct 20 '22 15:10

Rikitikitaco