Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Spring MVC and Thymeleaf

Thymeleaf is a modern server-side Java template engine for web and standalone environments. This guide covers the key concepts and steps for integrating Thymeleaf with Spring MVC, including setting up Thymeleaf, creating Thymeleaf templates, and using Thymeleaf features in your Spring MVC application.

Key Concepts of Spring MVC and Thymeleaf

  • Thymeleaf Template Engine: A template engine that processes HTML, XML, JavaScript, CSS, and text.
  • ThymeleafViewResolver: A Spring MVC ViewResolver implementation that uses Thymeleaf templates.
  • Thymeleaf Expressions: Special syntax used to process templates, such as variable expressions, message expressions, and URL expressions.

Setting Up Thymeleaf

Include the necessary dependencies for Thymeleaf in your pom.xml file:

<dependencies>
    <!-- Spring Boot Starter Thymeleaf -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <!-- Spring Boot Starter Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Configuring Thymeleaf

Configure Thymeleaf in your Spring MVC application by defining a ThymeleafViewResolver bean:

WebConfig.java

// WebConfig.java
package com.example.springmvc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

@Configuration
public class WebConfig {

    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setPrefix("/WEB-INF/views/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        return templateResolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        return templateEngine;
    }

    @Bean
    public ThymeleafViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        return viewResolver;
    }
}

Creating Thymeleaf Templates

Create Thymeleaf templates in the src/main/webapp/WEB-INF/views directory:

home.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Home</title>
</head>
<body>
    <h1>Home</h1>
    <p th:text="${message}">Welcome to Spring MVC with Thymeleaf!</p>
</body>
</html>

Creating the Controller

Create a controller to handle requests and use Thymeleaf templates:

HomeController.java

// HomeController.java
package com.example.springmvc.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/home")
    public String home(Model model) {
        model.addAttribute("message", "Welcome to Spring MVC with Thymeleaf!");
        return "home";
    }
}

Using Thymeleaf Features

Thymeleaf provides various features for template processing, such as variable expressions, message expressions, and URL expressions:

Variable Expressions

<p th:text="${message}">Welcome to Spring MVC with Thymeleaf!</p>

Message Expressions

<p th:text="#{welcome.message}">Welcome message</p>

URL Expressions

<a th:href="@{/home}">Home</a>

Key Points

  • Thymeleaf Template Engine: A template engine that processes HTML, XML, JavaScript, CSS, and text.
  • ThymeleafViewResolver: A Spring MVC ViewResolver implementation that uses Thymeleaf templates.
  • Thymeleaf Expressions: Special syntax used to process templates, such as variable expressions, message expressions, and URL expressions.
  • Include the necessary dependencies for Thymeleaf in your pom.xml file.
  • Configure Thymeleaf in your Spring MVC application by defining a ThymeleafViewResolver bean.
  • Create Thymeleaf templates in the src/main/webapp/WEB-INF/views directory.
  • Create a controller to handle requests and use Thymeleaf templates.
  • Use Thymeleaf features such as variable expressions, message expressions, and URL expressions to process templates.

Conclusion

Integrating Thymeleaf with Spring MVC allows you to create dynamic and interactive web applications with ease. By setting up Thymeleaf, creating Thymeleaf templates, and using Thymeleaf features, developers can enhance their Spring MVC applications and provide a rich user experience. Happy coding!