java Spring-MVC:需要最简单的表单处理、绑定和验证示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4112149/
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-10-30 04:48:24  来源:igfitidea点击:

Spring-MVC: Need the most simple example of form-processing, binding, and validation

javadata-bindingspring-mvcvalidation

提问by David Parks

I have a form:

我有一个表格:

<form action="/processform">
   <input name="firstname" value="john" />
   <input name="lastname" value="doe" />
</form>

I have a Person object:

我有一个 Person 对象:

public class Person {
   private String firstname;
   private String lastname;
   // ... getters & setters ...
}

I want to receive this data, perform validation on it, and post it to a datastore.

我想接收这些数据,对其进行验证,然后将其发布到数据存储区。

How do I write a controller to do this? I understand that I could pass the parameters in as request parameters, but I think the "proper" way to do this is somehow bind the data from the form to the Person object and then receive that Person object in the controller and call a Validate object that is configured to receive the Person object.

我如何编写控制器来做到这一点?我知道我可以将参数作为请求参数传递,但我认为这样做的“正确”方法是以某种方式将数据从表单绑定到 Person 对象,然后在控制器中接收该 Person 对象并调用一个 Validate 对象配置为接收 Person 对象。

After much reading, this step has confounded me. Can someone show me what is needed to "bind" the data, "validate" (e.g. a validator), and "process" the data (e.g. the controller, and in particular what gets passed to it as parameters)?

经过大量阅读,这一步让我感到困惑。有人可以告诉我“绑定”数据、“验证”(例如验证器)和“处理”数据(例如控制器,特别是作为参数传递给它的内容)需要什么?

回答by David Parks

Here was the answer I was looking for, I didn't understand that Spring, by default, will take all of the parameters from the form submission (such as "firstname" and "lastname") and can create the object for you by calling the setter methods of these parameters.

这是我正在寻找的答案,我不明白 Spring 在默认情况下会从表单提交中获取所有参数(例如“名字”和“姓氏”),并且可以通过调用为您创建对象这些参数的setter方法。

The controller:

控制器:

@Controller
public class MyFormProcessor {
   @RequestMapping("/formsubmit")
   public String handleForm(@Valid Person person, BindingResult errors, Map<String,Object> model){
      // ...handle form...
   }
}

Spring is essentially doing the following magic before calling handleFormfor this request (obviously in a more extendable way than I depict for this simple example):

在调用handleForm这个请求之前,Spring 本质上做了以下魔法(显然比我为这个简单的例子描述的更可扩展):

Person person = new Person();
person.setFirstname( request.getParameter("firstname") );
person.setLastname( request.getParameter("lastname") );
handleForm(person, anErrorsObject, new Model());

For validation you can either create your own validator (which I won't mention anything about here), or if you include Hibernate Validator in the classpath, then you can annotate the Personclass (example below) and when you add the @Validannotation as I depicted in the example above the Hibernate validator will validate the class based on those annotations and post any errors to the error object (a BindingResultobject is an extension of Springs' Errors, and for simple examples the Errorsobject is the interesting component).

对于验证,您可以创建自己的验证器(我不会在这里提及任何内容),或者如果您在类路径中包含 Hibernate 验证器,那么您可以注释Person该类(下面的示例),并且当您添加@Valid注释时,如我所描述的在上面的示例中,Hibernate 验证器将根据这些注释验证类并将任何错误发布到错误对象(BindingResult对象是 Springs' 的扩展Errors,对于简单示例,Errors对象是有趣的组件)。

JSR-303 validation annotated Person class (for use with the @Valid option):

JSR-303 验证注释的 Person 类(与 @Valid 选项一起使用):

public class Person {
   @NotNull
   @Size(min=3, max=20)
   private String firstname;

   @NotNull
   @Size(min=3, max=20)
   private String lastname;

   // ... getters & setters ...
}

回答by Bozho

Spring has a complete tutorial showing every aspect that you need. It's called "Petclinic". You can check it out from:

Spring 有一个完整的教程,展示了您需要的各个方面。它被称为“宠物诊所”。您可以从以下位置查看:

git https://github.com/SpringSource/spring-petclinic

git https://github.com/SpringSource/spring-petclinic