Getting Started with Thymeleaf in SpringBoot

Similar to how .NET MVC frameworks utilize Razor syntax with cshtml files for server-side code integration, SpringBoot offers comparable view rendering capabilities. When developing MVC applications with SpringBoot, developers can leverage template engines that provide functionality akin to Razor.

Spring Boot includes several template engines out-of-the-box, including FreeMarker, Groovy, Thymeleaf, Velocity, and Mustache. Among these, Thymeleaf is the recommended choice due to its excellent integrasion with Spring MVC. As a Java library, Thymeleaf serves as a template engine for XML, XHTML, and HTML5 documents, functioning effectively as the View layer in MVC web applications. It also provides dedicated modules for seamless integration with Spring MVC, making it a viable alternative to JSP.

Thymeleaf Configuration

The configuration properteis available for Thymeleaf can be found in the ThymeleafProperties.class within org.springframework.boot.autoconfigure.thymeleaf. Following Spring Boot's convention-over-configuration principle, default values are already established in this class. Should you wish to modify these defaults, adjustments can be made in the application.properties file. This example uses all default settings, with templates stored in the templates directory and formatted as HTML files.

Adding Thymeleaf to Your Project

This example builds upon a previous SpringBoot tutorial. To integrate Thymeleaf, add the following dependency to your pom.xml file. Note that since Spring 5 is being used, ensuring the correct Thymeleaf vertion is crucial to prevent errors. Different Spring versions may require different artifact IDs for Thymeleaf.

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <version>3.0.9.RELEASE</version>
        </dependency>

Additionally, when working with HTML pages, include the following declaration in your HTML document:

<html xmlns:th="http://www.thymeleaf.org">

Implementation Example

In this demonstration, we create a Controller and an HTML template. All Thymeleaf properties use their default values, but customizations can be made through the application.properties file under the spring.thymeleaf prefix.

package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


//@RestController
@Controller
public class HelloController {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(Model model) {
        model.addAttribute("name", "Cuiyw");
        return "hello";
    }
}


<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello!, ' + ${name} + '!'" ></p>
</body>
</html>

Tags: SpringBoot thymeleaf java web-development mvc

Posted on Sat, 06 Jun 2026 16:29:36 +0000 by dr-dre67