Spring Boot Validation
- 영상 링크
- JSR 380: Bean Validation 2.0
- JSR 303: Bean Validation 1.0
Form
@PostMapping("/greeting")
public String greetingSubmit(@ModelAttribute Greeting greeting, Model model) {
model.addAttribute("greeting", greeting);
return "result";
}
public class Greeting {
private long id;
private String content;
// getters and setters
}
<form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
<p>Id: <input type="text" th:field="*{id}"/></p>
<p>Message: <input type="text" th:field="*{content}"/></p>
<p><input type="submit" value="Submit"/> <input type="reset" value="Reset"/></p>
</form>
Validation
public class PersonForm {
@NotNull
@Size(min=2, max=30)
private String name;
@NotNull
@Min(18)
private Integer age;
// getters and setters
}
@PostMapping("/")
public String checkPersonInfo(@Valid PersonForm personForm, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "form";
}
return "redirect:/results";
}
<form action="#" th:action="@{/}" th:object="${personForm}" method="post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" th:field="*{name}" /></td>
<td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</td>
</tr>
<tr>
<td>Age:</td>
<td><input type="text" th:field="*{age}" /></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>
Validation Annotations JSR380
@NotNull
@AssertTrue
@Size
@Min
@Max
@Email
@NotEmpty
: not null, not empty of String, Collection, Map or Array values
@NotBlank
: not null or no whitespace
@Positive
, @PositiveOrZero
@Negative
, @NegativeOrZero
@Past
, @PastOrPresent
: date value
@Future
, @FutureOrPresent
List<@NotBlank String> preferences;
ref