Spring Boot

Spring Boot Thymeleaf Example

Thymeleaf is a template engine which exercises the HTML or XML file, parses it and then produces web content.

The primary difference between JSP and thymeleaf is JSPs compile to Servlets.

However, Thymeleaf reads the template file, parses its DOM and then applies the model to it.

In this tutorial, we will take a look at how to use thymeleaf in spring boot application.

More Spring Boot tutorials

Spring Boot Security Example.
Spring Validator
Spring Boot REST Example
Spring Boot Swagger 2 Example

Features of Thymeleaf

1. Integration with Spring is easy
2. Simple to use.
3. Support Spring Expression Language (SpEL)
4. Integration with Spring Security is easy
5. Cache support
6. Internationalization  support

Let’s develop a demo using the Spring Boot web and Thymeleaf.

Thymeleaf configuration

At first, let’s see how to configure thymeleaf into spring boot.

Let’s create a new project in Spring Tool Suite 4 and add dependencies for Web and Thymeleaf dependencies.

Adding thymeleaf in spring boot is easy. You don’t have to perform any configuration. Just adding spring-boot-starter-thymeleaf in pom.xml is sufficient.

Project’s dependency tree

Spring boot Thymeleaf example
Spring Boot Thymeleaf Example

As you could see that spring automatically downloaded Thymeleaf 3.0.11 version into the project’s classpath.

In addition to that when spring boot finds the thymeleaf jars in the classpath, then it automatically processes and configures it with default properties.

In order to change configuration properties, you can specify values in the application.properties or application.yml file.

If you want to use a specific version of thymeleaf then you can specify version details in the properties tag of pom.xml.

Important Thymeleaf Properties

Let’s take a look into a few important properties.

  • spring.thymeleaf.cache: enable/disable template caching 
  • spring.thymeleaf.prefix: add the prefix name to the view name before building the URL.
  • spring.thymeleaf.suffix: add the suffix name to the view name before building the URL.
  • spring.thymeleaf.check-template: Before rendering, check whether the template exists or not.
  • spring.thymeleaf.encoding: Specifies the template encoding
  • spring.thymeleaf.content-type: Specifies the content type

Create Controller Class

A controller can handle the incoming request, prepares the model data and return the view (HTML) to render greet.html.

As you could notice from the above class GreetingController is a Controller class that maps incoming HTTP GET request with a view.

The above GreetingController class can process “/” and “/greet” request.

@GetMapping maps /greet to greet() method.

In addition to that, the ‘name’ is a query string parameter of /greet request.

Let’s take a look at the view template

View Template

Firstly create a greet.html in the src/main/resources folder.

The below file is a simple HTML5 template with some thymeleaf related modifications.



Now, let’s run this program to see the output.

spring boot thymeleaf
spring boot thymeleaf example