SpringMVC
eclipse
JSR 303是java为bean数据合法性校验提供的标准框架,它已经包含在javaee6.0中。JSR 303通过在Bean属性上标注类似于@NotNull、@Max等标准的注解指定校验规则,并通过这些标准的验证接口对Bean进行验证。
Hibernate Validator是JSR 303的一个参考实现,除支持所有的标准校验注解外,她还支持一下的扩展注解。
Spring4.0拥有自己独立的数据校验框架,同时支持JSR 303标准的校验框架。Spring在进行数据绑定时,课通过调用校验框架完成数据校验工作。在SpringMVC中,课直接通过注解驱动的方式进行数据校验。Spring的LocalValidatorFactoryBean既实现了Spring的Validator接口,也实现了JSR 303的Validator接口。只要在Spring容器中定义了一个LocalValidatorFactoryBean,即可将注入到需要的数据校验的Bean中。Spring本身并没有提供JSR 303的实现,所以必须将JSR 303的实现者jar包放在类路径下。在maven配置文件中加入Hibernate Validator的依赖包。
在bean的属性上添加对应的注解。package com.gwolf.springmvc.domain;import java.util.Date;import javax.validation.constraints.Past;import org.hibernate.validator.constraints.Email;import org.hibernate.validator.constraints.NotEmpty;import org.springframework.format.annotation.DateTimeFormat;import org.springframework.format.annotation.NumberFormat;public class Employee { private Integer id; @NotEmpty private String lastName; @Email private String email; //1 male, 0 female private Integer gender; private Department department; @Past @DateTimeFormat(pattern='yyyy-MM-dd') private Date birth; @NumberFormat(pattern='#,###,###.#') private Float salary; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Integer getGender() { return gender; } public void setGender(Integer gender) { this.gender = gender; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public Float getSalary() { return salary; } public void setSalary(Float salary) { this.salary = salary; } @Override public String toString() { return 'Employee [id=' + id + ', lastName=' + lastName + ', email=' + email + ', gender=' + gender + ', department=' + department + ', birth=' + birth + ', salary=' + salary + ']'; } public Employee(Integer id, String lastName, String email, Integer gender, Department department) { this.id = id; this.lastName = lastName; this.email = email; this.gender = gender; this.department = department; } public Employee() { }}
通过在控制层的处理方法入参上标注@Valid注解即可让SpringMVC在完成数据绑定后执行数据校验的工作。package com.gwolf.springmvc.handlers;import java.util.Map;import javax.validation.Valid;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.validation.BindingResult;import org.springframework.validation.Errors;import org.springframework.validation.FieldError;import org.springframework.web.bind.WebDataBinder;import org.springframework.web.bind.annotation.InitBinder;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import com.gwolf.springmvc.dao.DepartmentDao;import com.gwolf.springmvc.dao.EmployeeDao;import com.gwolf.springmvc.domain.Employee;@Controllerpublic class EmployeeHandler { @Autowired private EmployeeDao employeeDao; @Autowired private DepartmentDao departmentDao; @RequestMapping('/testConversionServiceConverter') public String testConverter(@RequestParam('employee') Employee employee, BindingResult bindingResult) { System.out.println('save:' + employee); employeeDao.save(employee); return 'emps'; } @RequestMapping(value='/emp', method=RequestMethod.POST) public String save(@Valid Employee employee,BindingResult bindingResult){ System.out.println('save: ' + employee); if(bindingResult.getErrorCount() > 0) { System.out.println('出错了'); for(FieldError fieldError : bindingResult.getFieldErrors()) { System.out.println(fieldError.getField() + ':' + fieldError.getDefaultMessage()); } } employeeDao.save(employee); return 'redirect:/emps'; } /*@InitBinder public void initBinder(WebDataBinder webDataBinder) { webDataBinder.setDisallowedFields('lastName'); }*/ @ModelAttribute public void getEmployee(@RequestParam(value='id',required=false) Integer id, Map
提交数据校验表单,可以看到后台的错误提示信息。
若验证出错,则转向定制的页面。