Spring常用校验API

从Spring 3.0开始,在Spring MVC中提供了对Java校验API的支持。在Spring MVC中要使用它的话,只要保证在类路径下包含这个Java API的实现即可,比如Hibernate Validator。

Java校验API定义了多个注解,这些注解可以放到属性上,从而限制这些属性的值。所有注解都位于javax.validation.constraints包中。下表列出了这些校验注解
Spring常用校验API_第1张图片

package spittr.model;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import  org.apache.commons.lang3.builder.EqualsBuilder;
import  org.apache.commons.lang3.builder.HashCodeBuilder;

public class Spitter {
     
    private Long id;

    @NotNull
    @Size(min=5, max=16)
    private String username;

    @NotNull
    @Size(min=5, max=25)
    private String password;

    @NotNull
    @Size(min=2, max=30)
    private String firstName;

    @NotNull
    @Size(min=2, max=30)
    private String lastName;

    public Spitter() {
     }

    public Spitter(String username, String password, String firstName, String lastName) {
     
        this(null, username, password, firstName, lastName);
    }

    public Spitter(Long id, String username, String password, String firstName, String lastName) {
     
        this.id = id;
        this.username = username;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getUsername() {
     
        return username;
    }

    public void setUsername(String username) {
     
        this.username = username;
    }

    public String getPassword() {
     
        return password;
    }

    public void setPassword(String password) {
     
        this.password = password;
    }

    public Long getId() {
     
        return id;
    }

    public void setId(Long id) {
     
        this.id = id;
    }

    public String getFirstName() {
     
        return firstName;
    }

    public void setFirstName(String firstName) {
     
        this.firstName = firstName;
    }

    public String getLastName() {
     
        return lastName;
    }

    public void setLastName(String lastName) {
     
        this.lastName = lastName;
    }

    @Override
    public boolean equals(Object that) {
     
        return EqualsBuilder.reflectionEquals(this, that, "firstName", "lastName", "username", "password");
    }

    @Override
    public int hashCode() {
     
        return HashCodeBuilder.reflectionHashCode(this, "firstName", "lastName", "username", "password");
    }
}

@RequestMapping(value="/register", method=POST)
public String processRegistration(
    @Valid Spitter spitter,   // 校验 Spitter输入
    Errors errors) {
     
    if (errors.hasErrors()) {
     
        return "registerForm";   // 如果校验出现错误,则重新返回表单
    }
    spitterRepository.save(spitter);
    return "redirect:/spitter/" + spitter.getUsername();
}

Spitter参数添加了@Valid注解,这会告知Spring,需要确保这个对象满足校验限制。

如果校验出现错误的话,那么这些错误可以通过Errors对象进行访问,现在这个对象已作为processRegistration()方法的参数。(很重要一点需要注意,Errors参数要紧跟在带有@Valid注解的参数后面,@Valid注解所标注的就是要检验的参数。)processRegistration()方法所做的第一件事就是调用Errors.hasErrors()来检查是否有错误。

如果有错误的话,Errors.hasErrors()将会返回到registerForm,也就是注册表单的视图。如果没有错误的话,Spitter对象将会通过保存,控制器会像之前那样重定向到基本信息页面。

你可能感兴趣的:(SSM框架)