Spring Boot with Thymeleaf tutorial, part 1

In this tutorial I am going to show you how to set up your first project using Spring Boot. I will use Maven and Thymeleaf as a template engine.

1. Create new project using Spring Tool Suite. Click File – New – Spring Starter Project. Fill inputs as in the picture below. Click Next button. If you do not use Spring Tool Suit you can use web version of this generator on http://start.spring.io.

1_spring_start

2. On the next check Thymeleaf as a template engine. Click Finish.

2_select_thymeleaf

3. Spring Starter Project generated new Maven project with pom.xml where are added all needed dependencies, especially spring-boot-starter-thymeleaf.

 

We have got now project with very simple structure as below:

3_projectstructure

Class SpringBootThymeleafApplication is responsible for running the project and it looks like below:

 

4. Usually Spring Boot application created in this way is ready to run, but using a Thymeleaf needs a little bit more effort. We have to create a new template file in templates directory. Create index.html file.

 

4_index

5. The last step is creating a controller which is able to show us the index.html page. Create new package “controller” and then new Home class in this package.

5_home_controller

Add @Controller stereotype to the class and @RequestMapping(“/”) to intex() method whick return the name of template.

 

6. No you can run SpringBootThymeleafApplication class as a Java Application in STS IDE or execute

and then

 

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

6_hello_world_browser

Complete source code: https://github.com/jvmhub/Spring-Boot-with-Thymeleaf-tutorial