Je n'ai pas bien compris le comportement de JSR-303 Bean Validation, ce qui a semé la confusion sur le terrain, je vais donc essayer de l'organiser. Plus précisément, les annotations suivantes. · Javax.validation.constraints.NotBlank (@NotBlank) · Javax.validation.constraints.NotEmpty (@NotEmpty) · Javax.validation.constraints.NotNull (@NotNull)
Veuillez noter que si tous les espaces sont des espaces pleine largeur, la vérification ne sera pas interceptée.
J'ai essayé de le vérifier sur l'écran simple suivant.
HelloController.java
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
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
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
<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>
Seuls NotBlank et NotEmpty restent bloqués dans le chèque
c'est tout.
Recommended Posts