使用Spring Boot Thymeleaf整理@ NotBlank,@ NotEmpty和@NotNull的行为差异


我想做什么

JSR-303我不太了解Bean验证的行为,这在该领域引起了混乱,因此我将尝试对其进行组织。具体来说,是以下注释。
?javax.validation.constraints.NotBlank(@NotBlank)
javax.validation.constraints.NotEmpty(@NotEmpty)
?javax.validation.constraints.NotNull(@NotNull)

结论

wwwww.PNG
*除制表符外,我尚未验证转义序列,但我认为它们的行为可能与制表符相同(可能)

请注意,如果所有空格都是全角空格,则不会捕获支票。

验证

我试图通过以下简单屏幕进行验证。

HelloController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.demo.form.HelloForm;

@Controller
@RequestMapping(value = "/")
public class HelloController {

    private static final String SAMPLE_URL = "sample/Hello";

    @GetMapping
    public String index(@ModelAttribute HelloForm helloForm) {
        return SAMPLE_URL;
    }

    @PostMapping
    public String register(@Validated HelloForm helloForm, BindingResult result) {
        return SAMPLE_URL;
    }
}

HelloForm.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.example.demo.form;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

public class HelloForm {

    @NotBlank
    String notBlankField;

    @NotEmpty
    String notEmptyField;

    @NotNull
    String notNullField;

    public String getNotBlankField() {
        return notBlankField;
    }

    public void setNotBlankField(String notBlankField) {
        this.notBlankField = notBlankField;
    }

    public String getNotEmptyField() {
        return notEmptyField;
    }

    public void setNotEmptyField(String notEmptyField) {
        this.notEmptyField = notEmptyField;
    }

    public String getNotNullField() {
        return notNullField;
    }

    public void setNotNullField(String notNullField) {
        this.notNullField = notNullField;
    }
}

Hello.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
Hello
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <form method="post" action="/" th:object="${helloForm}">
        <span th:if="${#fields.hasErrors('notBlankField')}" th:errors="*{notBlankField}" style="color: red"></span><br />
        NotBlankField:<input type="text" name="notBlankField" th:field="*{notBlankField}" /><br />
        <span th:if="${#fields.hasErrors('notEmptyField')}" th:errors="*{notEmptyField}" style="color: red"></span><br />
        NotEmptyField:<input type="text" name="notEmptyField" th:field="*{notEmptyField}" /><br />
        <span th:if="${#fields.hasErrors('notNullField')}" th:errors="*{notNullField}" style="color: red"></span><br />
        NotNullField:<input type="text" name="notNullField" th:field="*{notNullField}" /><br /><br />
        <input type="submit" value="Submit" />
    </form>
</body>
</html>

空字符串

仅检查NotBlank和NotEmpty
空文字.PNG

所有半角空格

*选中"仅非空白"
全て半角スペース.PNG

所有全角空格

*不空白,不为空,不为Null均未选中
全て全角スペース.PNG
来源如下。
https://github.com/kenichi-nagaoka/spring-boot-form-validation-sample

这就是

的全部。