Signup/Sign In

Spring MVC with Thymeleaf

In this article, we will learn to use Thymeleaf in our Spring application. The Thymeleaf is a template engine that is used to create view pages in Spring MVC. It provides full Spring integration and was designed to replace JSP as a view page in Spring MVC. It is better to use Thymeleaf if you are working with a web application.

It works in both web and non-web applications and can process any XML file even in offline environments. To use Thymeleaf in your project just add this dependency to your pom.xml file.


Let's understand by an example by creating a maven based spring project. Our project structure contains several files and looks like below.

Project Structure


This is a configuration file in Java which is an alternate of the applicationContext.xml file that we created for the XML-based configuration example. The @Configuration annotation indicates that this is not a simple class but a configuration class and the @ComponentScan annotation is used to indicate the location of the component class in our spring project.

package com.studytonight;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import com.studytonight.models.User;

public class AppConfig implements WebMvcConfigurer{

	ApplicationContext applicationContext;

	public ViewResolver thymeleafResolver() {
		ThymeleafViewResolver ivr = new ThymeleafViewResolver();
		return ivr;

	public SpringResourceTemplateResolver templateResolver() {
		SpringResourceTemplateResolver srtr = new SpringResourceTemplateResolver();
		return srtr;

	public SpringTemplateEngine templateEngine() {
		SpringTemplateEngine templateEngine = new SpringTemplateEngine();
		return templateEngine;



This file contains code to create an IOC container for our application. The AnnotationConfigApplicationContext class is used to create an object for application context.

package com.studytonight;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.servlet.DispatcherServlet;
public class MainApp implements WebApplicationInitializer {

	public void onStartup(ServletContext servletContext) throws ServletException { 
		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context));


This is a controller class that contains two methods showForm() and showdata(). The showForm() method is mapped with userForm request and used to return a user-form. Here, we used Spring Model to pass user_name to view(user-data.jsp).

package com.studytonight.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.studytonight.models.User;

public class HomeController {
	public String hello() {		
		return "index";
	public String thyme(Model model) {
		model.addAttribute("user", new User());
		return "thyme";


It is our User bean class that contains getters and setters methods.

package com.studytonight.models;
public class User {
	int id;
	String user_name;
	String email;
	public String getUser_name() {
		return user_name;
	public void setUser_name(String user_name) {
		this.user_name = user_name;
	public int getId() {
		return id;
	public void setId(int id) { = id;
	public String getEmail() {
		return email;
	public void setEmail(String email) { = email;

// thyme.html

This is an HTML file that displays a form by using the Thymeleaf library. Here, we used <html xmlns:th=""> tag to include the Thymeleaf in our HTML page.

<!DOCTYPE html>
<html xmlns:th="">
<meta charset="ISO-8859-1">
<title>Spring Thymeleaf</title>
<h2>User Form</h2>
<label>Enter User Name</label><br>
<input type="text" th:field="*{user.user_name}" /><br><br>
<label>Enter Email</label><br>
<input type="email" th:field="*{}" /><br><br>
<input type="submit">

// pom.xml

This file contains all the dependencies of this project such as spring jars, servlet jars, etc. Put these dependencies into your project to run the application.

<project xmlns=""
		<!-- -->


Run the Application

After successfully completing the project and adding the dependencies run the application and you will get the output as below.

About the author:
I am a Java developer by profession and Java content creator by passion. I have over 5 years of experience in Java development and content writing. I like writing about Java, related frameworks, Spring, Springboot, etc.