常用参数验证注解 | 含义 |
---|---|
@NotNull | 不能为空 |
@Pattern(regex=) | 字符串必须匹配正则表达式 |
@Size(min=,max=) | 集合的元素数量必须在min和max之间 |
@Length(min=,max=) | 检查字符串的长度 |
必须是email地址 | |
@NotBlank | 字符串必须有字符(空的字符串和null都报错) |
@NotEmpty | 字符串不为空,集合必须有元素 |
@Range(min=,max=) | 数值必须在min和max之间 |
@SafeHtml | 字符串是安全的html(动态拼接的html防止黑客注入) |
@URL | 合法的URL |
@AssertFalse | Boolean型校验 :必须是false |
@AssertTrue | Boolean型校验 :必须是true |
@DecimalMax(value=,inclusive=) | 作用于数字或者字符串意思是x i n c l u s i v e = = t r u e ? < = {inclusive}==true? <= inclusive==true?<={value} : <${value} |
@DecimalMin(value=,inclusive=) | 作用于数字或者字符串意思是x i n c l u s i v e = = t r u e ? > = {inclusive}==true? >= inclusive==true?>={value} : >${value} |
@Digits(integer=,fraction=) | 数字格式检查,integer表示数字整数位最大多少位,fraction指定小数位最大多少位 |
@Max(value=) | 数字格式检查,<=${value} |
@Min(value=) | 数字格式检查,>=${value} |
@Futrue | 必须是未来的时间 |
@Past | 必须是过去的时间 |
用法:将以上的注解加到实体类上,在controller参数列表上加上 BindingResult 需要验证的参数前加@Valid