Blog Image

Spring with Thymeleaf  

Thymeleaf is a Java template engine for processing and creating HTML, XML, JavaScript, CSS and Text. Let's explore how to integrate Spring MVC application with Thymeleaf. Thymeleaf makes development faster than using Jsp's.

Here we will integrate Spring MVC application with Thymeleaf.

Technologies Used

  • Spring 5.0.2.RELEASE
  • Java 8
  • Thymeleaf 3.0.9.RELEASE
  • Tomcat 9

Project Structure

Maven Dependencies

<project xmlns=""; xmlns:xsi="";
    <name>thymeleaf Maven Webapp</name>

Below is the configuration required for integrating Spring + Thymeleaf

To integrate Spring + Thymeleaf we need SpringResourceTemplateResolver, this class will identify the Thymeleaf template and mode of the Template as HTML. SpringTemplateEngine used templateResolver.

Then we need ThymeleafViewResolver to resolve logical viewName to the Thymeleaf Template. Below is the configuration for the above three classes.

<bean id="templateResolver"
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML5" />

    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />

    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
        <property name="viewNames" value="*.html,*.xhtml" />

Model object to bind the data to display in the jsp page

package com.thymeleaf.dto;

public class Account {
    protected int accountNo;
    protected String accountHolderName;
    protected String accountType;

    public Account(int accountNo, String accountHolderName, String accountType) {
        this.accountNo = accountNo;
        this.accountHolderName = accountHolderName;
        this.accountType = accountType;

    public int getAccountNo() {
        return accountNo;

    public void setAccountNo(int accountNo) {
        this.accountNo = accountNo;

    public String getAccountHolderName() {
        return accountHolderName;

    public void setAccountHolderName(String accountHolderName) {
        this.accountHolderName = accountHolderName;

    public String getAccountType() {
        return accountType;

    public void setAccountType(String accountType) {
        this.accountType = accountType;


Controller for binding data to be rendered as part of Thymeleaf view

package com.thymeleaf.controller;

import java.util.ArrayList;
import java.util.List;

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

import com.thymeleaf.dto.Account;

public class ListAccountsController {
    public String showAccounts(Model model) {
        List<Account> accounts = null;

        /* adding some dummy accounts to display in view */
        accounts = new ArrayList<>();
        accounts.add(new Account(1, "Susan", "savings"));
        accounts.add(new Account(2, "Brain", "current"));

        model.addAttribute("accounts", accounts);
        return "accounts.html";

Finally let's create the Thymeleaf HTML page to render the above data.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">;
<html xmlns:th="">;
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <table th:if="${not #lists.isEmpty(accounts)}">
                <th>Account Holder Name</th>
            <tr th:each="account:${accounts}">
                <td th:text="${account.accountHolderName}">Account Holder Name</td>
                <td th:text="${account.accountType}">Type</td>

web.xml configured with DispatcherServlet

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 ""; >

  <display-name>Archetype Created Web Application</display-name>
  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->

    <!-- Map all requests to the DispatcherServlet for handling -->

After deploying the application we can access it using http://localhost:8080/thymeleaf/accounts.htm
Refer to the example attached to this blog

Simple Attributes

model.addAttribute("message", "Welcome to Thymeleaf");

<span th:text="${message}">This is a message</span>

Conditional Statements

  • If and Unless

 <span th:if="${account.accountType} == 'savings'" th:text="ATM withdraw only"></span>
    <span th:unless="${account.accountType} == 'savings'" th:text="ATM and Check withdraw">

  • Switch and case


  <td th:switch="${account.accountType}">
          <span case="savings" th:text="ATM withdraw only"/>
          <span case="current" th:text="ATM and check withdraw"/>

About author

User Image

A software developer on JDK and JEE platform. I am passionate about Java technology and always an explorer and learner in new technologies in Java. I have experience on Open Source technologies like Struts, Spring, Jsf etc.. and even strong knowledge on Integration/Distributed world like Ejb, Web Services and Restful Services.



Be the first person to write a comment for this Blog
  • author image
      25-12-2017 06:22:14 PM

    thank u for writing for this content . It is so useful content for us.

  • author image
      23-12-2017 04:10:24 AM

    Thanque for writing this blog it is use full for me.

  • author image
      21-12-2017 11:47:06 AM

    helpful content,Thank u ! Plz give some info about how to add static resources(css,js,images...) with thymeleaf into project.

Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.