在pom.xml,添加validator验证器的依赖

springMVC中使用 JSR-303验证器( Validation 接口 )

    <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.13.Final</version>
      </dependency>

新建一个Cat类

package com.oukele.model;

import javax.validation.constraints.*;

public class Cat {

    @NotBlank//验证字符串非空
    private String id;//猫的编号

    @NotBlank//验证字符串非空
    private String name;//猫的名字

    @Max(value = 5)//最大年龄
    @Min(value = 1)//最小年龄
    private int age;//猫的年龄

    @Pattern(regexp = "^[0-9]{11}$")//电话的格式
    private String tel;//铲屎官的电话\

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", tel='" + tel + '\'' +
                '}';
    }
}
View Code

相关文章:

  • 2022-12-23
  • 2022-02-09
  • 2021-12-01
  • 2021-10-25
  • 2021-11-14
  • 2022-12-23
  • 2022-03-04
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-27
  • 2021-10-10
相关资源
相似解决方案