校验数据

从Spring 3.0开始提供对Java校验API(Java Validation API,又称JSR-303)的支持

Java校验API所提供的校验注解(所有注解都在javax.validation.constraints 包下)

image-20190731224204395

使用实例

Spitter 简单POJO

public class Spitter {

    // 非空,5-16个字符
    @NotNull
    @Size(min = 5,max = 16)
    private String username;

}

processRegister注册方法

@RequestMapping(value = "/register",method = RequestMethod.POST)
public String processRegister(@Valid Spitter spitter, Errors errors){
    if (errors.hasErrors()){
        return "registerForm";
    }
    ...
}

results matching ""

    No results matching ""