Springboot 数据校验

 1.pom.xml 文件

     <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.daqu</groupId>
    <artifactId>springboot_7</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_7</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.7.Final</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.创建实体类 domian层  Users.java    

@NotBlank: 非空校验

@NotBlank: 判断字符串是否为 null 或者是空串(去掉首尾空格)。

@NotEmpty: 判断字符串是否 null 或者是空串。

@Length: 判断字符的长度(最大或者最小)

@Min: 判断数值最小值

@Max: 判断数值最大值

@Email: 判断邮箱是否合法

这几个校验就是 我们今天的重点.

public class Users {
    @NotBlank //非空校验
    private String name;
    @NotBlank //非空校验
    private String password;

    private Integer age;

    @Override
    public String toString() {
        return "Users{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

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

    public Integer getAge() {
        return age;
    }

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

controller 层创建UsersController.java

@Controller
public class UsersController {

    @RequestMapping("/addUser")
    public  String showPage(@ModelAttribute("aa")  Users users){
        return "add";
    }
      /*
      *
      * 需求: 如果没有填写相关信息 页面不跳转*/
    @RequestMapping("/save")
    public String  saveUser (@ModelAttribute("aa") @Valid Users users, BindingResult result){

        if (result.hasErrors()){
            return "add";
        }
        System.out.println(users);
        return "ok";
    }
}

3.启动器


@SpringBootApplication
public class Springboot7Application {

    public static void main(String[] args) {
        SpringApplication.run(Springboot7Application.class, args);
    }

}

4.页面 当页面选框填写为空就会一直返回到输入页面. 输入正确的信息,就跳转到添加成功的页面.

template下创建

add.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
</head>
<body>
      <form th:action="@{/save}" method="post">
          用户姓名: <input type="text" name="name"/> <font color="red" th:errors="${aa.name}"></font>
          用户密码: <input type="text" name="password"/><font color="red" th:errors="${aa.password}"></font>
          用户年龄: <input type="text" name="age"/><font color="red" th:errors="${aa.age}"></font>
          <input type="submit" value="ok"/>
      </form>

</body>
</html>

ok.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加成功</title>
</head>
<body>
添加成功
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值