📜  Spring – MVC 验证

📅  最后修改于: 2022-05-13 01:54:29.522000             🧑  作者: Mango

Spring – MVC 验证

Spring MVC 框架为我们提供了标准的预定义验证器,以简单直接的方式验证用户输入数据。 Bean Validation API 是 Spring 应用程序中数据验证的流行方法。在这里,我们将使用称为 Hibernate Validator 的 Bean Validation API 的 hibernate 实现。

验证注释

以下是一些最常用的验证注解。

1. @Min(value=) – 检查注释值是否大于或等于指定的最小值。

2. @Max(value=) – 检查注释值是否小于或等于指定的最大值。 '

@Min(value = 18, message = "Age must be greater than 18")
@Max(value = 25, message = "Age must be smaller than 25")
private int age;

3. @NotNull – 检查带注释的值不为空。

4. @NotBlank – 检查带注释的字符序列/字符串不为空且修剪长度大于 0。

5. @NotEmpty – 检查带注释的元素不为空且不为空。

// @NotNull: The CharSequence, Collection, Map or Array object is not null, but can be empty.
// @NotEmpty: The CharSequence, Collection, Map or Array object is not null and size > 0.
// @NotBlank: The string is not null and the trimmed length is greater than zero.

@NotEmpty(message = "First name cannot be null and must have size greater than 0")
private String firstName;

@NotNull(message = "Second name must not be null, empty value/space can be considered")
private String lastName;

@NotBlank(message = "Username must not be null and must contain 1 or more characters")
private String userName;

6. @Email – 检查指定的字符序列/字符串是否是有效的电子邮件地址。

@Email(message = "Email should be valid")
private String email;

7. @Pattern(regex=, flags=) – 检查带注释的字符串是否与给定标志匹配的正则表达式匹配。

// The regex specifies that the password can contain characters from a to z, A to Z and 0-9 only,
// also it must be in between 6 to 10 characters long.

@Pattern(regexp = "^[a-zA-Z0-9]{6,10}$")
private String password;

8. @AssertFalse – 检查带注释的元素是否为假。

9.@AssertTrue—— 检查带注释的元素是否为真。

@AssertTrue 
private boolean isWorking;

@AssertFalse
private boolean isStudent;

10. @NegativeOrZero – 检查给定元素是否小于或等于 0。

11. @Null - 检查带注释的值是否为空。

12. @Negative - 检查元素是否严格小于 0。

13. @Positive - 检查元素是否严格大于 0。

14. @PositiveOrZero – 检查给定元素是否大于或等于 0。

@Positive
private int operand1;

@Negative
private int operand2;

@PositiveOrZero
private int operand3

@NegativeOrZero
private int operand4;

@Null
private int nullVal;

15. @Size – 检查注释元素的大小是否在提供的最小值和最大值之间(包括)。

@Size(min = 10, max = 200, message = "About Me must be between 10 and 200 characters")
private String aboutMe;

示例 1:用户类

Java
// Java Program to Illustrate Calculator class
 
package com.example.springmvc;
 
// Importing required classes
import javax.validation.constraints.*;
import lombok.Data;
 
// Annotation
@Data
 
// Class
public class User {
 
    @NotEmpty(
        message
        = "First name cannot be null and must have size greater than 0")
    private String firstName;
 
    @NotNull(
        message
        = "Second name must not be null, empty value/space can be considered")
    private String lastName;
 
    @NotBlank(
        message
        = "Username must not be null and must contain 1 or more characters")
    private String userName;
    ;
 
    @AssertTrue private boolean working;
 
    @Min(value = 18,
         message = "Age must be greater than 18")
    @Max(value = 25,
         message = "Age must be smaller than 25")
    private int age;
 
    @Size(
        min = 10, max = 200,
        message
        = "About Me must be between 10 and 200 characters")
    private String aboutMe;
 
    @Email(message = "Email should be valid")
    private String email;
 
    @Pattern(regexp = "^[a-zA-Z0-9]{6,10}$")
    private String password;
 
    @AssertTrue private boolean isWorking;
 
    @AssertFalse private boolean isStudent;
}


Java
// Java Program to Illustrate Calculator class
 
package com.example.springmvc;
 
// Importing required classes
import javax.validation.constraints.*;
import lombok.Data;
 
// Annotation
@Data
// Class
public class Calculator {
 
    // Operand4 can contain values > 0 only
    @Positive private int operand1;
 
    // Operand4 can contain values <  only
    @Negative private int operand2;
 
    // Operand4 can contain values >= 0 only
    @PositiveOrZero private int operand3;
 
    // Operand4 can contain values <= 0 only
    @NegativeOrZero private int operand4;
 
    // Value of operator must be assigned at runtime.
    @Null private char operator;
}


XML


    org.hibernate.validator
    hibernate-validator
    6.2.0.Final


Java
// https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator
implementation group : 'org.hibernate.validator',name : 'hibernate-validator', version: '6.2.0.Final'
// Or
// implementation
// 'javax.validation:validation-api:2.0.1.Final'


示例 2:计算器类

Java

// Java Program to Illustrate Calculator class
 
package com.example.springmvc;
 
// Importing required classes
import javax.validation.constraints.*;
import lombok.Data;
 
// Annotation
@Data
// Class
public class Calculator {
 
    // Operand4 can contain values > 0 only
    @Positive private int operand1;
 
    // Operand4 can contain values <  only
    @Negative private int operand2;
 
    // Operand4 can contain values >= 0 only
    @PositiveOrZero private int operand3;
 
    // Operand4 can contain values <= 0 only
    @NegativeOrZero private int operand4;
 
    // Value of operator must be assigned at runtime.
    @Null private char operator;
}


依赖

XML



    org.hibernate.validator
    hibernate-validator
    6.2.0.Final


Java

// https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator
implementation group : 'org.hibernate.validator',name : 'hibernate-validator', version: '6.2.0.Final'
// Or
// implementation
// 'javax.validation:validation-api:2.0.1.Final'