Thymeleaf is a simple library that allows to run the HTML templates in Java applications. In this blog, I am going to create a simple web form within a Java project in order to get some inputs via thee web page. This blog will be very summarised to see how the approach is simple
Steps need to be done
Step 1-Create a simple java project via maven
Step 2-Add the following libraries to the maven, we are going to use Spring Boot Starter. The main libraries are spring-boot-starter-web and thymeleaf
Step 3-Create an Application with the tag @SpringBootApplication this will be the starting point for the project
Step 4-Create a resource → templates folder to store HTML files under it
Step 5-As a starting point, we are going to use a simple HTML form
The following output will represent the HTML file
Step 6-Create a java controller to read the form and process it
As you see, name parameter is the same for the form as well as the java controller (IndexController.java). What the application is doing to get the user name from index.html and print the following output
Name parameter is fetched for ….
Step 7-Run the java application from Application.java
Step 8-Open the browser and run localhost:8080
When you filled out the name and click “Show Name”, you can see the following response
As you see, we created a simple web form within the Java project.
Thymeleaf is a very useful library when you need to add simple web forms to a Java-based application. One of the good thing is that you don’t need to add any server which makes this approach more efficient.