Spring MVC application flow and integration with Thymeleaf

Thymeleaf is a Java template engine for XML, XHTML and HTML5 that works best at the view layer of MVC based web applications just as JSP. Thymeleaf has already proved to be a great alternative to JSP.

Note that, Thymeleaf is Open-Source Software, licensed under the Apache License 2.0 and the latest stable release  is 2.1.4.

We will be digging much deeper into Thymeleaf but let’s first list out the steps to integrate Thymeleaf with Spring MVC. Even before that, let’s have a brief overview about the Spring MVC application flow, beautifully depicted by the below flow diagram:Integration of Thymeleaf with Spring MVC

  1. Handler mapping maps all the request mappings in the controllers. This happens at server startup.
  2. Based on the request that gets hit, dispatcher servlet asks the handler mapping as to which equivalent controller method needs to be invoked.
  3. The controller after completing it’s task finally returns a view, model and view or a simple string to the dispatcher servlet.
  4. The dispatcher servlet then checks with the view resolver and finally resolves the view returned in step 3, integrates the model to the view and renders it on the browser.

Now Thymeleaf enters at the view level.

The traditional bean configuration for a JSP/JSTL ViewResolver looks something like:

What we would be using in case of Thymeleaf along-side Spring MVC should look something like:

There is the another template resolver, ServletContextTemplateResolver, that can be used alternatively. However, it can’t be used if the View components are externalized. In that case, we need to use FileTemplateResolver.

Template Resolvers:

  1. InternalResourceViewResolver is used to resolve views that lie internal to the project under WEB-INF directory, in order to protect them from direct access by manually entering the URL. This is specifically used when using JSP as the view component.
  2. FileTemplateResolver is an implementation of IResourceResolver that resolves the resources as files in the filesystem. This is used intentionally to keep all my Thymeleaf templates externally in the filesystem, so that I can change it as and when needed, similar to any HTML file.
  3. ServletContextTemplateResolver helps in resolving resources as servlet context resources and interprets relative paths within the web application root directory.

Template engine in a broader sense provides the parsing of the Thymeleaf templates and provides a standard dialect with several attribute processors and features.

We need a separate .jar file (thymeleaf-spring4-{version}.jar), which has to be added to the classpath in order to integrate with the Spring MVC application. This will in turn allow the controller methods to return templates managed by Thymeleaf, use Spring Expression Language to resolve model data, display internationalization messages from message files managed by Spring (through the usual MessageSource objects) and finally after parsing these templates by the template engine (SpringTemplateEngine), the resolved template shows up on the browser screen.

If using Spring MVC setup is with Maven, below are the pom file entries that needs to be added:

There are a few advantages of Thymeleaf over JSP including some listed below:

  1. The Expression Language used (actually called Standard Dialect) is much more powerful than JSP Expression Language.
  2. It is an open-source project and has a pretty good documentation.
  3. With Thymeleaf, we can keep all our Thymeleaf templates externally in the file system, so that we can change it as and when needed, similar to any HTML file.

So all you need to do is, add the above bean configurations into your Spring configuration file and start off to explore your technical skills with Thymeleaf. Now that you got the slightest idea of Thymeleaf, there are several other planned articles on Thymeleaf that we are bringing up for you soon on jCombat.

There are other official documentations available on Thymeleaf that you can check out:

Thymeleaf Documentation

Special thanks to my friends Jimish Bhayani and Priyanka Anand for making this article release possible.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.