加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:29:15 所属栏目:大数据 来源:网络整理
导读:在我的示例中,嵌套对象时忽略@DateTimeFormat注释: class Person { private Date birthdate; // other fields @DateTimeFormat(pattern="dd.MM.yyyy") public Date getBirthdate(){ return birthdate; } // Other getters/setters } 我有一个嵌套这个对象的

在我的示例中,嵌套对象时忽略@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中,我使用打印嵌套变量:

但不幸的是,输入字段中的日期值格式不正确(但原则上显示日期).
当我直接将类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 
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读