Spring Boot - Valid Form表单参数验证

Spring Boot - Valid Form表单参数验证

本文介绍使用@Valid优雅的进行Form表单参数校验,避免大量的if(){...}语句…

主要依赖

lombok插件主要为了简化代码,自行视情况添加使用。
thymeleaf主要为了页面测试

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

创建实体类

创建一个Person实体类:

@Data
public class Person {

    @NotNull
    @Size(min = 4, max = 16, message = "用户名须在4-16位之间")
    private String userName;

    @NotNull
    @Min(value = 18, message = "年龄最小18岁")
    private Integer age;
}
  • @NotNull用来标示字段非空;
  • @Size(min = 4, max = 16, message = "用户名须在4-16位之间")用来限定字符长度, 这个表示字符长度在4-16之间, message用于校验失败时的提示信息;
  • @Min(value = 18, message = "年龄最小18岁")表示最小值时18, 不允许小于18;

创建Controller

新建一个Controller用于测试

@Controller
public class DemoController {

    @GetMapping("/")
    public String formPage(Person person){
        return "form";
    }

    @PostMapping("/")
    public String checkParams(@Valid Person person, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return "form";
        }
        return "success";
    }
}
  • @Valid用于收集属性进行参数校验;
  • BindingResult可以进行测试检索验证错误,返回验证结果信息;

创建HTML页面

新建一个HTML页面(form.html)用于表单提交

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Params Valid Demo</title>
</head>
<body>
<form action="#" th:action="@{/}" th:object="${person}" method="post">
    <table>
        <tr>
            <td>UserName:</td>
            <td><input type="text" th:field="*{userName}" th:autocomplete="off"/></td>
            <td th:if="${#fields.hasErrors('userName')}" th:errors="*{userName}">UserName Error</td>
        </tr>
        <tr>
            <td>Age:</td>
            <td><input type="text" th:field="*{age}" th:autocomplete="off"/></td>
            <td th:if="${#fields.hasErrors('age')}" th:errors="*{age}">Age Error</td>
        </tr>
        <tr>
            <td><button type="submit">Submit</button></td>
        </tr>
    </table>
</form>
</body>
</html>

创建一个成功页面(success.html),用于校验通过跳转:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h3>SUCCESS.</h3>
</body>
</html>

运行并测试

运行启动类:

@SpringBootApplication
public class BootValidApplication {
    public static void main(String[] args) {
        SpringApplication.run(BootValidApplication.class, args);
    }
}
  • 启动后,打开表单页,输入不合参数并提交:

  • 输入正确的参数提交:

完整代码

Github 码云
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章