Spring Boot with Thymeleaf tutorial, part 2 – Forms with validation

In this tutorial I am going to extend previous project and show you how to handle with Forms in Spring Boot and Thymeleaf. I am going to create simple form for post. If post is valid, it will be displayed on the result view.

1. Set up the project as is described in previous post or checkout the source code.

2. pom.xml file is the same like in the previous project. I have only changed name and description.

3. Create a Post object. It will be used by framework to correspond with form. We can also define validation constraints here.


4. Controller has two actions now. First one displays empty form and second one processes data sent by user. It shows an error message if any validation constrain is not met. If all data is valid, it fills a model with data from form and shows a result page.


5. The last step is to create two HTML files. First of them (“index.html”) contains a form with bindings with Post object. It also contains a places where errors can be displayed. Second file (“result.html”) just shows a data from form if it is valid.


All file structure should look like this:


Compile the application by

mvn package

and run it by

java -jar target/spring-boot-thymeleaf-form-0.0.1.jar

Your application is available on http://localhost:8080




Complete source code: https://github.com/jvmhub/Spring-Boot-with-Thymeleaf-part-2-forms-with-validation