Integrating Thymeleaf Views with Spring MVC

[Updated: Sep 20, 2017, Created: Aug 13, 2017]

Thymeleaf is a Java template engine which can process HTML, XML, JavaScript, CSS, plain text etc. Thymeleaf can be integrated with Spring MVC as views. Let's see a quick example to learn how to do that.


Maven dependency



The first artifact (thymeleaf) is Thymeleaf framework and the second one (thymeleaf-spring4) contains Spring integration components.

Thymeleaf configuration

public class MyWebConfig {
  ApplicationContext applicationContext;
  public SpringTemplateEngine templateEngine(){
      SpringTemplateEngine templateEngine = new SpringTemplateEngine();
      return templateEngine;
  public SpringResourceTemplateResolver templateResolver(){
      SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
      return templateResolver;
  public ViewResolver viewResolver(){
      ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
      return viewResolver;

All above components are provided by Thymeleaf framework for Spring integration. Following is a quick getting-to-know description of them:

  • SpringTemplateEngine

    This class hooks up an instance of SpringStandardDialect (a subclass of org.thymeleaf.standard.StandardDialect). Thymeleaf dialects define a set of template processors.

  • SpringResourceTemplateResolver

    It resolves templates using Spring's resource resolution mechanism (see ApplicationContext.getResource(String)).

  • ThymeleafViewResolver

    It is an implementation of org.springframework.web.servlet.ViewResolver. The View implementation managed by this ViewResolver is ThymeleafView which is responsible to carry out template processing.

Writing a Spring Controller

public class MyController {
  public String handleRequest (Model model) {
      model.addAttribute("msg", "A message from the controller");
      return "my-page";

The Thymeleaf template


<!DOCTYPE html>
<html xmlns=""

   <h2>A Thymeleaf view</h2>
   <div th:text="${msg}"/>
   <div th:text="${time}"/>

As seen above the Model attributes are applied as Thymeleaf specific attributes (details here).


The generated html:

<!DOCTYPE html>
<html xmlns="">

   <h2>A Thymeleaf view</h2>
   <div>A message from the controller</div>

Example Project

Dependencies and Technologies Used:

  • spring-webmvc 4.3.10.RELEASE: Spring Web MVC.
  • thymeleaf 3.0.7.RELEASE: Modern server-side Java template engine for both web and standalone environments.
  • thymeleaf-spring4 3.0.7.RELEASE: Modern server-side Java template engine for both web and standalone environments.
  • javax.servlet-api 3.1.0 Java Servlet API
  • JDK 1.8
  • Maven 3.3.9

Using Thymeleaf View in Spring MVC Select All Download
  • spring-thymeleaf-view-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • webapp
          • WEB-INF
            • views

See Also