First application with Spring Boot and Thymeleaf

1. Introduction

Having gone through the Spring Boot introduction and first time Spring Boot project setup in Eclipse IDE, we are ready to create our first Spring Boot application with Thymeleaf. Now that we have Spring Boot, you will notice how minimal configuration we would be doing.

Need help in setting up Spring Boot in Eclipse IDE? Click here

2. Implementation

With the Spring Boot initial project setup, you will notice that the SpringBootDemoApplication class is already available inside the project, which is actually the starting point of your application.

2.1 Understanding the Main SpringBootDemoApplication class

Note that, @SpringBootApplication is equivalent to using the three annotations in Spring, namely – @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes.

  • @EnableAutoConfiguration automatically configures our Spring application based on the dependencies that we have specified.
  • @ComponentScan is helps in locating the spring beans that would be needed in our application.

2.2 Understanding the POM Dependencies

Getting over to the pom.xml file, the default pom that is generated could be seen inheriting from the spring-boot-starter-parent project, which basically provides –

  • All the needed Maven defaults.
  • Additionally, no extra burden of managing the version of dependencies. All default versions are implicitly mapped, which you can override only when it is needed.
  • Introduces the run goal, which could be used to start the application (using mvn spring-boot:run)

You will also note that the pom.xml also has web starter spring-boot-starter-web, which is required in case of a web application or if you are creating a RestFul Service.

spring-boot-starter-web uses Tomcat as the default embedded container. So whenever you need to build a web application, this is the starter that you need.

Another dependency that I will be needing for this demo application would be the Thymeleaf starter (spring-boot-starter-thymeleaf), which will take away all the extra burdens to configure template engine and view resolvers, which we had always been doing earlier.

Then is the spring-boot-starter-actuator that might always be required any way, which provides additional features to help us monitor and manage our application when it’s pushed to production.

So the final pom for my demo application will be having the following dependencies –

Another starter namely spring-boot-starter-test, can also be added that would help in testing my application using JUnit, Hamcrest and Mockito libraries. This starter also generates an empty class template named, which can be readily used for writing test cases using the test libraries.

2.3 Writing the Controller

Now lets create our DemoController and map the root application path to the view welcome.html. For the demo application, let’s keep the controller as simple as mentioned below –

So we have the model object ready. Let’s try to retrieve the model data inside the Thymeleaf template.

Let’s create welcome.html and put it inside the designated location \src\main\resources\templates\

By default Spring boot serves static content from src/main/resources/, which we can change as per our requirement by adding the spring.resources.staticLocations property in file.


2.4 Running the Spring Boot application

This is all and we are done. No additional web.xml configurations are required. Just need to run the application as –

Run spring boot application

Once you run it, you will see the welcome page rendering elegantly with the model values appearing successfully inside the dropdown.

Spring Boot Thymeleaf output

In the first time Spring Boot setup, you will also notice spring-boot-maven-plugin inside the pom file, which is responsible for creating executable JARs. Executable jars contain all the required libraries it needs to run, so that you can run it on command line using traditional java -jar myapp.jar.

3. Download the source code

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.