Spring Boot, Thymeleaf Hello World (Spring Boot + Thymeleaf + JS/CSS)
- 4.6/5
- 5338
- Jul 20, 2024
In this article we will see how to create a "Spring Boot" web project with "Thymeleaf", we will also see how to import "css", "js" and "images" and populate dynamic data.
1) Dependencies
Let's create a project with , make sure to add required dependencies as shown in the picture below:
The final "pom.xml" should look something like this:
2) Static Resources
We should add static resources like: "js", "css" and "images" in "src/main/resources/static" folder and "Thymeleaf(.html)" files in "src/main/resources/templates" folder as shown in the image below:
app.css
index.html
3) Controller
This is the place to define "requestMappings", please note how "Model" is used to send data from "Controller" to "Thymeleaf's" .html file.
4) Service
This is just an abstraction between the data layer and the controller. We are generating some fake data here to show it on the UI, but in a real scenario all the data comes from a datasource.
5) Model
This is a simple model; we used it to send "employee" data from controller layer to view layer (thymeleaf).
6) Spring Boot
This is plain old Spring Boot driver class; all the execution starts from here.
7) Testing
Now we are all done with our project setup; lets run the application and navigate to home page : http://localhost:8080