To handle form submissions with Thymeleaf in Spring, put a form object in the model on the GET request, bind it in the template with th:object and th:field, then accept it in a @PostMapping method annotated with @Valid followed by a BindingResult. If the result has errors, return the form view so Thymeleaf shows the messages. Otherwise process the data and redirect.
The example uses Spring Boot 3.x, Java 21 and Jakarta Bean Validation. It uses a Java record as the form object, which Spring can bind through its canonical constructor.
Add the web, Thymeleaf and validation starters. Validation is no longer included in the web starter, so the third dependency is required.
Spring Boot 3 uses the jakarta.validation packages. Older code with javax.validation imports must be migrated.
The form object carries the submitted values and the constraints. A record keeps it short and immutable.
The no-argument constructor lets the controller create an empty form for the first GET request. If you prefer a classic JavaBean with getters and setters, that works the same way.
th:object binds the form to the model attribute, and th:field generates the matching id, name and value. th:errors prints the validation messages for a field.
Save it as templates/register.html. The novalidate attribute turns off browser validation so you can see the server messages while learning. In production keep both layers, because client checks are only a convenience. Thymeleaf also inserts the CSRF hidden field when Spring Security is active.
The BindingResult parameter must come directly after the @Valid object, otherwise Spring throws an exception instead of passing the errors to you.
On a validation error the method returns the view name, and the submitted values and messages are shown again. On success it redirects, which follows the post/redirect/get pattern and prevents duplicate submissions when the user reloads the page.
Flash attributes survive one redirect, so name is available here only for the first load of the page.
For rules that annotations cannot express, such as checking that an email is not already taken, add a field error manually in the controller before the hasErrors check.
rejectValue attaches the message to the field, and the same th:errors expression displays it. For reusable rules, write a custom constraint annotation with a ConstraintValidator.
The BindingResult parameter must immediately follow the validated model attribute in the method signature. Putting another parameter between them causes the exception.
th:field generates id, name and value from the bound object and keeps the user's input after a failed submission. Plain attributes require you to do that yourself.
Use th:each="err : ${#fields.allErrors()}" with th:text="${err}". Keep field-level messages near the inputs for accessibility.
No. This pattern is for server-rendered pages. For JSON APIs, use @RequestBody with @Valid and return error details as JSON.
Thymeleaf forms in Spring use a model attribute, th:object and th:field for binding, @Valid with BindingResult for validation, and th:errors for messages. Redirect after a successful submission, keep server validation even when the browser validates too, and use rejectValue for business rules.