【问题标题】:Spring MVC : How To Get BindingResult Error Field for Nested ObjectSpring MVC:如何获取嵌套对象的 BindingResult 错误字段
【发布时间】:2012-02-20 04:17:21
【问题描述】:

我提交此数据以通过 Ajax (POST) 添加子实体:

(实体类定义见本题底部)

name = "Child Name"
parent.id = 3

一切正常。新的子实体已成功保存。

但如果不包含parent.id(仅设置name)(使用POST方法提交)

name = "Child Name"

验证结果返回这个 JSON:

"errors":{"parent":"may not be null"}

注意该 JSON 中的 "parent" 属性。它应该返回parent.id 而不是parent

这会导致问题,因为客户端脚本 (HTML) 上的字段名称为 "parent.id" 而不是 "parent"

任何建议如何返回parent.id 而不是parent ??

这里是处理方法:

@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
public Map<String, ?> add(@Valid Child child, BindingResult result) {

    Map<String, ?> out = new LinkedHashMap<String, ?>();

    if(result.hasErrors()){
        Map<String, String> errors = new LinkedHashMap<String, String>();
        for (FieldError error : result.getFieldErrors()) {
           errors.put(error.getField(), error.getDefaultMessage());
        }
        out.put("success", false);
        out.put("errors", errors);
        return out;
    } else {
        out.put("success", true);
    }

    return out;

}

这里是实体类:

class Child {
    private int id;

    @NotNull
    @Size(min = 5)
    private String name;

    @NotNull        
    private Parent parent;

    //getter and setter methods
}

class Parent {
    private int id;

    @NotNull
    private String name;

    //getter and setter methods
}

谢谢。

【问题讨论】:

    标签: json validation data-binding spring-mvc


    【解决方案1】:

    感谢@oehmiche 的建议。

    我最终将实体类更改为这些:

    class Child {
        @NotNull
        private int id;
    
        @NotNull
        @Size(min = 5)
        private String name;
    
        @NotNull
        @Valid
        private Parent parent = new Parent("Foo Foo");
    
        //getter and setter methods
    }
    
    class Parent {
        @NotNull
        private int id;
    
        @NotNull
        private String name;
    
        public Parent(){
        }
    
        public Parent(String name){
             setName(name);
        }
    
        //getter and setter methods
    }
    

    这是提交的数据:

    id = 1
    name = "Child Name"
    parent.id = 3
    

    注意id 属性。我最终总是将其设置为 1 只是为了绕过@NotNull 约束。虽然id 值总是会被​​ Hibernate 替换(我使用自动生成策略)。

    (我为父类的id 以及子类添加@NotNull 约束以保持一致性)

    但是,还有一个问题。我必须始终设置父级的名称只是为了绕过父级的name 属性的验证约束:

    @NotNull
    @Valid
    private Parent parent = new Parent("Foo Foo");
    

    对此有何建议?

    【讨论】:

    【解决方案2】:

    根据 hiberante 验证规范,对象图验证是使用要验证的属性上的 @Valid 触发的(在您的情况下是 parent 属性)。但也有人提到null 值将被忽略(请参阅example 下面的注释)。 所以在你的情况下,我建议在你的 Child 类中实例化一个空的 Parent 对象并用 @Valid 注释那个对象:

    @Valid        
    private Parent parent = new Parent();
    

    另见https://stackoverflow.com/a/5142960/160729

    【讨论】:

    • 谢谢@oehmiche。我认为您的解决方案是正确的。但现在它验证了 null parent.name 而不是 null parent.id。对此还有什么建议吗??
    猜你喜欢
    • 2018-05-19
    • 2017-06-27
    • 2016-11-24
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 2018-09-10
    相关资源
    最近更新 更多