Close

Integrating Thymeleaf Views with Spring MVC

[Last Updated: Sep 20, 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.

Example

Maven dependency

pom.xml

<dependency>
   <groupId>org.thymeleaf</groupId>
   <artifactId>thymeleaf</artifactId>
   <version>3.0.7.RELEASE</version>
</dependency>
<dependency>
   <groupId>org.thymeleaf</groupId>
   <artifactId>thymeleaf-spring4</artifactId>
   <version>3.0.7.RELEASE</version>
</dependency>

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

Thymeleaf configuration

@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig {
  
  @Autowired
  ApplicationContext applicationContext;
  
  @Bean
  public SpringTemplateEngine templateEngine(){
      SpringTemplateEngine templateEngine = new SpringTemplateEngine();
      templateEngine.setTemplateResolver(templateResolver());
      return templateEngine;
  }
  
  @Bean
  public SpringResourceTemplateResolver templateResolver(){
      SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
      templateResolver.setApplicationContext(this.applicationContext);
      templateResolver.setPrefix("/WEB-INF/views/");
      templateResolver.setSuffix(".html");
      return templateResolver;
  }
  
  @Bean
  public ViewResolver viewResolver(){
      ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
      viewResolver.setTemplateEngine(templateEngine());
      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

@Controller
@RequestMapping("/")
public class MyController {
  
  @RequestMapping
  public String handleRequest (Model model) {
      model.addAttribute("msg", "A message from the controller");
      model.addAttribute("time", LocalTime.now());
      return "my-page";
  }
}

The Thymeleaf template

src/main/webapp/WEB-INF/views/my-page.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

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

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

Output

The generated html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<body>
   <h2>A Thymeleaf view</h2>
   <div>A message from the controller</div>
   <div>21:43:35.050</div>
</body>
</html>

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
                • MyWebConfig.java
          • webapp
            • WEB-INF
              • views

    See Also