一:校验
1.校验的步骤
使用JSP 303验证标准
加入Hibernate validator的jar
在springmvc配置文件中加入<mvc:annotation-driven/>
需要在bean的属性上添加@Valid注解
2.添加lib
hibernate的lib
然后需要添加hibernate需要的required中的lib包
3.添加验证的注解
1 package com.spring.it.enties; 2 3 import java.util.Date; 4 5 import javax.validation.constraints.Past; 6 7 import org.hibernate.validator.constraints.Email; 8 import org.hibernate.validator.constraints.NotEmpty; 9 import org.springframework.format.annotation.DateTimeFormat; 10 import org.springframework.format.annotation.NumberFormat; 11 12 public class Employee { 13 14 private Integer id; 15 @NotEmpty 16 private String lastName; 17 @Email 18 private String email; 19 //1 male, 0 female 20 private Integer gender; 21 private Department department; 22 //birth 23 @Past 24 @DateTimeFormat(pattern="yyyy-MM-dd") 25 private Date birth; 26 //salary 27 @NumberFormat(pattern="###.#") 28 private Float salary; 29 30 public Employee(Integer id, String lastName, String email, Integer gender, 31 Department department) { 32 this.id = id; 33 this.lastName = lastName; 34 this.email = email; 35 this.gender = gender; 36 this.department = department; 37 } 38 39 public Employee() { 40 41 } 42 public Integer getId() { 43 return id; 44 } 45 46 public void setId(Integer id) { 47 this.id = id; 48 } 49 50 public String getLastName() { 51 return lastName; 52 } 53 54 public void setLastName(String lastName) { 55 this.lastName =lastName; 56 } 57 58 public String getEmail() { 59 return email; 60 } 61 62 public void setEmail(String email) { 63 this.email = email; 64 } 65 66 public Integer getGender() { 67 return gender; 68 } 69 70 public void setGender(Integer gender) { 71 this.gender = gender; 72 } 73 74 public Department getDepartment() { 75 return department; 76 } 77 78 public void setDepartment(Department department) { 79 this.department = department; 80 } 81 82 public Date getBirth() { 83 return birth; 84 } 85 86 public void setBirth(Date birth) { 87 this.birth = birth; 88 } 89 // 90 91 public Float getSalary() { 92 return salary; 93 } 94 95 public void setSalary(Float salary) { 96 this.salary = salary; 97 } 98 99 @Override100 public String toString() {101 return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender102 + ", department=" + department + ", birth=" + birth + ", salary=" + salary + "]";103 }104 105 }
4.保存上添加@Valid注解
1 /** 2 * 保存,是submit提交过来的请求,属于Post请求 3 */ 4 @RequestMapping(value="/emp",method=RequestMethod.POST) 5 public String save(@Valid Employee employee,BindingResult result) { 6 System.out.println(employee); 7 if(result.getErrorCount()>0) { 8 System.out.println("has error"); 9 for(FieldError error:result.getFieldErrors()) {10 System.out.println(error.getField()+":"+error.getDefaultMessage());11 }12 }13 employeeDao.save(employee);14 return "redirect:/emps";15 }
二:出错跳转页面
1.程序
这个主要的逻辑是save之后的处理。
要将department回显,所以save函数中要添加map。
1 /** 2 * 保存,是submit提交过来的请求,属于Post请求 3 */ 4 @RequestMapping(value="/emp",method=RequestMethod.POST) 5 public String save(@Valid Employee employee,BindingResult result,Mapmap) { 6 System.out.println(employee); 7 if(result.getErrorCount()>0) { 8 System.out.println("has error"); 9 for(FieldError error:result.getFieldErrors()) {10 System.out.println(error.getField()+":"+error.getDefaultMessage());11 }12 //如果出错,则转向定制的页面13 map.put("department", departmentDao.getDepartments());14 return "input";15 }16 employeeDao.save(employee);17 return "redirect:/emps";18 }
2.效果
就是提交之后,还是这个页面
三:在页面上显示这个错误信息
1.input程序
在每一个要验证的字段后面写上:<form:errors path="birth"></form:errors><br>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 4 <%@ page import="java.util.Map"%> 5 <%@ page import="java.util.HashMap"%> 6 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 7 8 9 10 1121 22 23 24 25 26 27Insert title here 12 13 14
2.效果
四:国际化资源文件
1.添加I18n.properties
注意写法:
1 NotEmpty.employee.lastName=\u4E0D\u80FD\u4E3A\u7A7A2 Email.employee.email=\u4E0D\u662F\u5408\u6CD5\u7684\u90AE\u4EF63 Past.employee.birth=\u4E0D\u80FD\u662F\u672A\u6765\u7684\u4E00\u4E2A\u65F6\u95F4
2.springmvc.xml中配置国际化
3.效果