[JAVA] Organize the differences in behavior of @NotBlank, @NotEmpty, @NotNull with Spring Boot + Thymeleaf

Thing you want to do

I don't understand the behavior of JSR-303 Bean Validation well, which caused confusion in the field, so I will try to organize it. Specifically, it is the following annotation. · Javax.validation.constraints.NotBlank (@NotBlank) · Javax.validation.constraints.NotEmpty (@NotEmpty) · Javax.validation.constraints.NotNull (@NotNull)

Conclusion

wwwww.PNG

Please note that if all spaces are full-width spaces, the check will not be caught.

Verification

I tried to verify it on the following simple screen.

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>

Empty string

Only NotBlank and NotEmpty get stuck in the check 空文字.PNG

All half-width spaces

All full-width space

that's all.

Recommended Posts

Organize the differences in behavior of @NotBlank, @NotEmpty, @NotNull with Spring Boot + Thymeleaf
See the relative redirect behavior with the server.tomcat.use-relative-redirects setting in Spring Boot
See the behavior of entity update with Spring Boot + Spring Data JPA
Specify the encoding of static resources in Spring Boot
Access the built-in h2db of spring boot with jdbcTemplate
Check the behavior of getOne, findById, and query methods in Spring Boot + Spring Data JPA
A story packed with the basics of Spring Boot (solved)
Use thymeleaf3 with parent without specifying spring-boot-starter-parent in Spring Boot
Thymeleaf usage notes in Spring Boot
Get a proxy instance of the component itself in Spring Boot
Coexistence of Flyway in the embedded database (h2) of the development environment and the release database (SQL Server) with Spring Boot
Procedure to make the value of the property file visible in Spring Boot
Loosen Thymeleaf syntax checking in Spring Boot
Implement CRUD with Spring Boot + Thymeleaf + MySQL
Implement paging function with Spring Boot + Thymeleaf
[Java] [Spring] Test the behavior of the logger
Run WEB application with Spring Boot + Thymeleaf
Get the path defined in Controller class of Spring boot as a list
Resource handler settings when delivering SPA with the static resource function of Spring Boot
How to set environment variables in the properties file of Spring boot application
Test controller with Mock MVC in Spring Boot
Asynchronous processing with regular execution in Spring Boot
The story of raising Spring Boot 1.5 series to 2.1 series
Let's check the feel of Spring Boot + Swagger 2.0
Create CRUD apps with Spring Boot 2 + Thymeleaf + MyBatis
Create your own Utility with Thymeleaf with Spring Boot
View the Gradle task in the Spring Boot project
I want to get the information of the class that inherits the UserDetails class of the user who is logged in with Spring Boot.
[Spring Boot] The story that the bean of the class with ConfigurationProperties annotation was not found
Include external jar in package with Spring boot2 + Maven3
I checked asynchronous execution of queries in Spring Boot 1.5.9
[For beginners] DI ~ The basics of DI and DI in Spring ~
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
Check the behavior of Java Intrinsic Locks with bpftrace
SSO with GitHub OAuth in Spring Boot 1.5.x environment
How to use CommandLineRunner in Spring Batch of Spring Boot
Create Restapi with Spring Boot ((1) Until Run of App)
Until you start development with Spring Boot in eclipse 1
How to boot by environment with Spring Boot of Maven
Check the behavior of include, exclude, ExhaustedRetryException of Spring Retry
Until you start development with Spring Boot in eclipse 2
Specify spring.profiles.active via context-param of web.xml in Spring Boot
Control the processing flow of Spring Batch with JavaConfig.
Database environment construction with Docker in Spring boot (IntellJ)
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
Branch processing by the return value of RestTemplate and the status code of ResponseEntity in Spring Boot
Let's find out how to receive in Request Body with REST API of Spring Boot
Download with Spring Boot
Spring Boot 1.x will reach EOL in the next year.
The story of raising Spring Boot from 1.5 series to 2.1 series part2
About the function of Spring Boot due to different versions
Change the injection target for each environment with Spring Boot 2
Put the file in the properties of string in spring xml configuration
Accelerate testing of Validators that require DI in Spring Boot
Extract SQL to property file with jdbcTemplate of spring boot
Until INSERT and SELECT to Postgres with Spring boot and thymeleaf
Try hitting the zip code search API with Spring Boot
Introduction of library ff4j that realizes FeatureToggle with Spring Boot
Find the number of days in a month with Kotlin
Login with HttpServletRequest # login in Spring Security of Servlet 3.x environment