📅  最后修改于: 2020-11-11 06:34:33             🧑  作者: Mango
以下示例显示了如何使用Spring Web MVC框架在表单中使用错误处理和验证器。首先,让我们拥有一个可用的Eclipse IDE,并遵循以下步骤来使用Spring Web Framework开发基于动态表单的Web应用程序。
Step | Description |
---|---|
1 | Create a project with the name TestWeb under a package com.tutorialspoint as explained in the Spring MVC – Hello World chapter. |
2 | Create Java classes Student, StudentController and StudentValidator under the com.tutorialspoint package. |
3 | Create view files addStudent.jsp and result.jsp under the jsp sub-folder. |
4 | Download Hibernate Validator library Hibernate Validator. Extract hibernate-validator-5.3.4.Final.jar and required dependencies present under the required folder of the downloaded zip file. Put them in your CLASSPATH. |
5 | Create a properties file messages.properties under the SRC folder. |
6 | The final step is to create the content of the source and configuration files and export the application as explained below. |
package com.tutorialspoint;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;
public class Student {
@Range(min = 1, max = 150)
private Integer age;
@NotEmpty
private String name;
private Integer id;
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
}
package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class StudentController {
@RequestMapping(value = "/addStudent", method = RequestMethod.GET)
public ModelAndView student() {
return new ModelAndView("addStudent", "command", new Student());
}
@ModelAttribute("student")
public Student createStudentModel() {
return new Student();
}
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public String addStudent(@ModelAttribute("student") @Validated Student student,
BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors()) {
return "addStudent";
}
model.addAttribute("name", student.getName());
model.addAttribute("age", student.getAge());
model.addAttribute("id", student.getId());
return "result";
}
}
NotEmpty.student.name = Name is required!
Range.student.age = Age value must be between 1 and 150!
在这里,键是
在这里,对于第一个服务方法student() ,我们在ModelAndView对象中传递了一个名称为“ command”的空白Studentobject> ,因为如果您使用的是
它将为所有输入验证呈现错误消息。我们正在使用路径=“ name”的
例如-
它将呈现错误消息以进行名称和年龄字段验证。
Spring MVC Form Handling
Submitted Student Information
Name
${name}
Age
${age}
ID
${id}
完成创建源文件和配置文件后,导出应用程序。右键单击您的应用程序,使用“导出”→“ WAR文件”选项,并将HelloWeb.war文件保存在Tomcat的webapps文件夹中。
现在,启动Tomcat服务器,并确保您能够使用标准浏览器从webapps文件夹访问其他网页。尝试输入URL- http:// localhost:8080 / TestWeb / addStudent ,如果您输入的值无效,我们将看到以下屏幕。