Spring 注解 @ModelAttribute 和 @Valid

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22658572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:37:42  来源:igfitidea点击:

Spring annotations @ModelAttribute and @Valid

springspring-mvcspring-dataspring-annotations

提问by vdenotaris

What are the advantages of using @ModelAttributeand @Valid?

使用@ModelAttribute和的优点是@Valid什么?

Which are the differences?

有哪些区别?

Is it possible to use them together?

可以一起使用吗?

回答by CodeChimp

@ModelAttributeis used to map/bind a a method parameter or method return type to a named model attribute. See @ModelAttributes JavaDoc. This is a Spring annotation.

@ModelAttribute用于将方法参数或方法返回类型映射/绑定到命名模型属性。请参阅@ModelAttribute的 JavaDoc。这是一个Spring注解。

@Validis an annotation that marks an object for JSR-303 bean validation. See @Valids JavaDoc. It is part of JavaEE 6, but I think Hibernate has an earlier implementation that most people use instead.

@Valid是标记对象以进行JSR-303 bean 验证的注解。请参阅@Valid的 JavaDoc。它是 JavaEE 6 的一部分,但我认为 Hibernate 有一个大多数人使用的早期实现。

The advantage of using @ModelAttributeis that you can map a form's inputs to a bean. The advantage of @Validis that you can utilize JSR-303 bean validation to ensure that the bean that is made is validated against some set of rules.

使用的好处@ModelAttribute是您可以将表单的输入映射到 bean。的优点@Valid是您可以利用 JSR-303 bean 验证来确保根据某些规则集验证生成的 bean。

Yes you can use @ModelAttributeand @Validtogether.

是的,你可以一起使用@ModelAttribute@Valid

The best way to transfer data from a form (sic View) to the a Model object is to follow the typical/traditional MVC design pattern using Spring. My personal preferred way is to have a form in a JSP with Spring JSTL <form:*>tags, with a modelAttributeset. On the Controller, have a handler to accept the POST from the form that has a matching @ModelAttributethat is a bean that represents the form's input. I would then pass that "Form Bean" to a service layer to do some things, including translating the "Form Bean" into any models if needed (not necessary if the form is creating your model objects directly) and saving/updating/etc via a DAO. This is but one way to do things, but it's probably the bulk of what I do with Spring in my day-to-day job.

将数据从表单 (sic View) 传输到 Model 对象的最佳方法是使用 Spring 遵循典型/传统 MVC 设计模式。我个人的首选方式是在带有 Spring JSTL<form:*>标签的 JSP 中使用一个表单modelAttribute。在控制器上,有一个处理程序来接受来自表单的 POST,该表单具有一个@ModelAttribute表示表单输入的 bean的匹配项。然后我会将“Form Bean”传递给服务层来做一些事情,包括在需要时将“Form Bean”翻译成任何模型(如果表单直接创建模型对象则不需要)并通过保存/更新/等一个 DAO。这只是做事的一种方式,但这可能是我在日常工作中使用 Spring 所做的大部分工作。

I would highlyrecommend reading the Spring reference materials and following the tutorials. The reference materials are very well written, easy to follow, and includes lots of examples on the various ways you can do things in Spring, and there are usually quite a few options on how you do things in Spring.

强烈建议阅读 Spring 参考资料并遵循教程。参考资料写得非常好,易于理解,并包含许多关于在 Spring 中做事的各种方式的示例,并且通常有很多关于如何在 Spring 中做事的选择。

回答by Bassem Reda Zohdy

please check the below part fro spring reference documentation:

请检查弹簧参考文档的以下部分:

In addition to data binding you can also invoke validation using your own custom validator passing the same BindingResult that was used to record data binding errors. That allows for data binding and validation errors to be accumulated in one place and subsequently reported back to the user:

除了数据绑定之外,您还可以使用您自己的自定义验证器来调用验证,该验证器传递用于记录数据绑定错误的相同 BindingResult。这允许将数据绑定和验证错误累积在一个地方,然后报告给用户:

@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result) {
    new PetValidator().validate(pet, result);
    if (result.hasErrors()) {
    return "petForm";
    }

    // ...
}

Or you can have validation invoked automatically by adding the JSR-303 @Valid annotation:

或者您可以通过添加 JSR-303 @Valid 注释来自动调用验证:

@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@Valid @ModelAttribute("pet") Pet pet, BindingResult result)             {
    if (result.hasErrors()) {
        return "petForm";
    }

    // ...

}