开发者

Spring MVC and JSR 303

I'm using Spring 3 and JSR 303. I have a form backing obje开发者_运维知识库ct whose nested objects need to be validated. In the example below, how do I validate formObject.getFoo().getBean()? When I run the code below, the result parameter is always empty, even if the HTML page submits nothing, when the validation should fail. Note that it works(i.e. the validation fails) when I validate it manually by calling validate(formObject.getFoo().getBean(), Bean.class).

@Controller
public class FormController {
    @RequestMapping(method = RequestMethod.POST)
    public void process(HttpServletRequest request, @Valid FormObject formObject, BindingResult result) {
            ...
    }

    // This is the class that needs to be validated.
    public class Bean {
        @NotBlank
        private String name;
    }

    public class Foo {
        private Bean bean;
    }

    public class FormObject {
        private Foo foo;
    }
}


If you want validation to cascade down into a child object, then you must put the @Valid annotation on the field in the parent object:

public class Bean {
    @NotBlank
    private String name;
}

public class Foo {
    @Valid
    private Bean bean;
}

public class FormObject {
    @Valid
    private Foo foo;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜