Thymeleaf Templating Engine for Web Application Views

Thymeleaf is a templating Engine, which will processes views which are in the form of HTML5/XHTML etc. These view templates will be parsed, cached, cloned and processes for each request. As it cached the templates, it will reduce lot of I/O operation time while displaying pages.
Note: Thymeleaf template engine will construct in memory DOM objects, hence we should avoid larger page templates.

Some of the features of thymeleaf…

1. Thymeleaf is a Java library. It is an XML/XHTML/HTML5 template engine able to apply a set of transformations to template files in order to display data and/or text produced by your applications.
2. Thymeleaf is a template engine framework, which will apply set of processors+data on a template and produce the web pages.
3. Processors + set of artifacts are called dialect in thymeleaf.
4. http://www.thymeleaf.org/doc/html/Using-Thymeleaf.html
5. The minimum goal of the thymeleaf is to provide an elegant and well – formed way of creating templates.
6. Its architecture allows fast processing of templates, relying on intelligent caching of parsed files which will reduce I/O operations.
7. Thymeleaf processing engine constructs the DOM objects from view templates.
8. These DOM objects will be cached in memory. Each time this DOM object will be cloned and processed to display the view. This will reduce IO operation time.

9. Anything that can be modeled as a DOM tree could effectively be processed as a template in thymeleaf.
10. Thymeleaf is an extremely extensible template engine (in fact it should be better called a template engine framework) that
allows you to completely define the DOM nodes that will be processed in your templates and also how they will be processed.
An object that applies some logic to a DOM node is called a processor, and a set of these processors –plus some extra
artifacts– is called a dialect.
11. Natural Template is possible in thymeleaf. where designer and developer will use same template without modifying.
12. all the request, session and application level variables will be accessed in OGNL style.
13. Supports internationalization of the application, where locale specific messages will be externalized to property files.

As of now Thymeleaf supports following formats,
Valid XML
Legacy HTML5

Highlevel view of thymeleaf processing
Construct in memory DOM structure from templates –> traverse each template Node –> apply processors on each Node (Even attribute/business Data to display) –> and produce final page.

How thymeleaf works in a simple web application

1. A servlet filter to direct all requests to the thymeleaf
ex: MyAppFilter
2. MyAppFilter will create thymeleaf resolver and template engine.

* thymeleaf resolver

ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();

* create template engine

templateEngine = new TemplateEngine();

* Even a messages resolver can be attached to thymeleaf to solve internationalization messages apart from out-of-the-box message resolver.
* And MyAppFilter will delegate each request to appropriate conroller

Controller controller = Application.resolveControllerForRequest(request);
            if (controller == null) {
                return false;

             * Obtain the TemplateEngine instance.
            TemplateEngine templateEngine = Application.getTemplateEngine();

             * Write the response headers
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);

             * Execute the controller and process view template,
             * writing the results to the response writer. 
                    request, response, this.servletContext, templateEngine);

Controller for path =”/” –> HomeController.java

 public class HomeController implements IGTVGController {

    public HomeController() {
    public void process(
            final HttpServletRequest request, final HttpServletResponse response,
            final ServletContext servletContext, final TemplateEngine templateEngine) 
            throws Exception {
        WebContext ctx = new WebContext(request, response, servletContext, request.getLocale());
        ctx.setVariable("today", Calendar.getInstance());
        templateEngine.process("home", ctx, response.getWriter());


Note: Detailed code can be found at http://www.thymeleaf.org/doc/html/Using-Thymeleaf.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s