微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Spring:在嵌套对象中忽略@DateTimeFormat

在我的示例中,嵌套对象时忽略@DateTimeFormat注释:

class Person {
    private Date birthdate;
    // other fields

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

我有一个嵌套这个对象的类.

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 
}

我在@ Controler-Method中将类型为PersonGroup的对象添加到我的模型中,如下所示:

model.addAttribute("myGroup",filledPersonGroup);

在JSP中,我使用打印嵌套变量:

modelattribute="myGroup" action="..." >
    

但不幸的是,输入字段中的日期值格式不正确(但原则上显示日期).
当我直接将类Person的实例添加到模型时,它可以工作.

任何人都可以告诉我如何处理这个问题?我希望我的嵌套对象的日期格式正确.

我使用Spring 4.1.1-RELEASE

最佳答案
您是否尝试过注释字段而不是方法

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

    public Date getBirthdate(){
       return birthdate;
    }
    // Other getters/setters 
}

原文地址:https://www.jb51.cc/spring/432209.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐