Spring MVC part III: ThymeLeaf integration

Today we are going to look into Thymeleaf, a very innovative and full HTML5-oriented templating engine.

Thymeleaf, by definition, can be used as a standalone engine but when associated with Spring MVC, it gives the best of its essence.

Once you start using Thymeleaf, it’s very hard to go back to the good old JSP. JSP is good, JSP served us well during a decade but now we’re in 2012 and HTML 5 is the future so it’s time to look ahead.

Please note that all the examples in this post can be found in a demo application on GitHub https://github.com/doanduyhai/ThymeLeafDemo

I Introduction

Unlike many Java templating engines of its kind, Thymeleaf is a full HTML engine, meaning that the template file itself is a HTML file that can be rendered by any Web browser.

How can it be possible ? Well, Thymeleaf is an attribute-based template engine, in the sense that all the language syntax relies heavily on HTML tag attributes. For example (see example1 in the tutorial code):

<html lang="en"
	xmlns="http://www.w3.org/1999/xhtml" 
	xmlns:th="http://www.thymeleaf.org">
	...
	...
<div class="container">
	<br/>
	<br/>
	<br/>
	<div class="span4 offset4 row-fluid">
		<div id="userMain" class="span1 table-bordered">
			<a href="#">
				<img id="picture" src="http://www.gravatar.com/avatar/"
					data-user="duyhai"
					th:src="${'http://www.gravatar.com/avatar/'+currentUser.gravatar+'?s=64'}"
					th:attr="data-user=${currentUser.login}">
				</img>
			</a>
		</div>
		<div class="span3 table-bordered">
			<h2>
				<span id="firstName" th:text="${currentUser.firstName}">DuyHai</span> 
				<span id="lastName" th:text="${currentUser.lastName}">DOAN</span>
			</h2>
		</div>
	</div>
</div>
</html>

As we can see at lines 15,16,22 & 23, the template file is a mere HTML file with some special attribute prefixed by the Thymeleaf namespace th. A web browser will simply ignore these additional attributes whereas the template engine will process them.

As fas as I know, the only other attribute-based template engine out there is TAL (Template Attribute Language) using Python language.

The fact that the template itself can be displayed in a web browser is an important feature. Indeed while developing an Web page, people first start designing the static part of the GUI (css, color, layout) before focusing on the dynamic part (data injection, conditional rendering). With JSP you start creating an empty web page template before injecting all the tag soup with the risk of altering the initial static design. With Thymeleaf there is no such headache, we do not need to add any new tag, just new attributes.

Below is an example of a static page design (see example2 in the tutorial code):

thymeleaf-static-design

As you can see, error and info message panels are all rendered at the same time and we also have some mocked search results.

At runtime, we get something similar to:

thymeleaf-runtime-result

All mocked lines have been removed, only true search results are displayed (we’ll see later in this post how mocks are removed).
 

II How does it work

All the examples in this chapter can be found in the demo application on GitHub https://github.com/doanduyhai/ThymeLeafDemo, as example1.

It’s time to see how Thymeleaf work under the hood. If we go back to the example 1

Below is the static rendering:

thymeleaf-exemple1-static

Now let’s focus in the template code:

<img id="picture" 
	src="http://www.gravatar.com/avatar/"
	data-user="duyhai"
	th:src="${'http://www.gravatar.com/avatar/'+currentUser.gravatar+'?s=64'}"
	th:attr="data-user=${currentUser.login}">
</img>

We can see there are 2 Thymeleaf attributes: th:src & th:attr. What happens at runtime is that Thymeleaf will parse your HTML document, pick any th attributes and perform substitution. Above, the src=”http://www.gravatar.com/avatar/” attribute will be replaced by an URL evaluated against the variable currentUser.gravatar (we’ll see soon where the variable comes from).

Similarly the <span id=”firstName” th:text=”${currentUser.firstName}”>DuyHai</span>
will be rendered as <span id=”firstName” >REAL_FIRSTNAME</span> at runtime, the existing DuyHai value is there only for the static preview of the template.

At runtime, we’ll get something similar to:

thymeleaf-exemple1-dynamic

The general rule is that all static content & attributes will be replaced by dynamic one at runtime by the engine.

At that time, you attentive reader should say: “hey Dude, it means there should be as many Thymeleaf attributes as there are HTML 5 tag attributes!” and you’re almost right, almost, because Thymeleaf also exposes additional attributes for flow control.

Below is a list (not comprehensive) of some existing attributes:

  • th:form
  • th:alt
  • th:action
  • th:href
  • th:src
  • th:title
  • th:value

Please report to the official documentation for a full list of availables attributes.

Last but not least, Thymeleaf can use several dialects. A dialect is a set of rules and expressions used to parse and process a template file. Thymeleaf is shipped by default with the Standard dialect only. If you use Thymeleaf as view engine for Spring then you should add an extra package to use Spring EL (SpEL) as default dialect.
 

III Configuration in Spring MVC

To use Thymeleaf with Spring MVC, you should first add the following dependencies in the pom.xml file:

  	&lt;dependency&gt;
  		&lt;groupId&gt;org.thymeleaf&lt;/groupId&gt;
  		&lt;artifactId&gt;thymeleaf&lt;/artifactId&gt;
  		&lt;version&gt;2.0.5&lt;/version&gt;
  		&lt;scope&gt;compile&lt;/scope&gt;
  	&lt;/dependency&gt;
  	&lt;dependency&gt;
  		&lt;groupId&gt;org.thymeleaf&lt;/groupId&gt;
  		&lt;artifactId&gt;thymeleaf-spring3&lt;/artifactId&gt;
  		&lt;version&gt;2.0.5&lt;/version&gt;
  		&lt;scope&gt;compile&lt;/scope&gt;
  	&lt;/dependency&gt; 

The second dependency is necessary to use Spring EL as default dialect.

Below is the Spring XML configuration for Thymeleaf:

&lt;bean id=&quot;templateResolver&quot; class=&quot;org.thymeleaf.templateresolver.ServletContextTemplateResolver&quot;&gt;
	&lt;property name=&quot;prefix&quot; value=&quot;/&quot; /&gt; 
	&lt;property name=&quot;suffix&quot; value=&quot;.html&quot; /&gt; 
	&lt;property name=&quot;templateMode&quot; value=&quot;HTML5&quot; /&gt; 
	&lt;property name=&quot;cacheable&quot; value=&quot;false&quot;/&gt;
&lt;/bean&gt;
	
&lt;bean id=&quot;templateEngine&quot; class=&quot;org.thymeleaf.spring3.SpringTemplateEngine&quot;&gt; 
	&lt;property name=&quot;templateResolver&quot; ref=&quot;templateResolver&quot; /&gt; 
&lt;/bean&gt;
		
&lt;bean class=&quot;org.thymeleaf.spring3.view.ThymeleafViewResolver&quot;&gt; 
	&lt;property name=&quot;templateEngine&quot; ref=&quot;templateEngine&quot; /&gt; 
	&lt;property name=&quot;characterEncoding&quot; value=&quot;UTF-8&quot;/&gt;
&lt;/bean&gt;

And that’s all! Please note that for development cycle, I suggest to set the cacheable flag to false so you can see real time updates of your template modifications. Of course this flag should be set to true in production.

The variables used in the template files (such as currentUser in the first example) are passed to Thymeleaf via the Model object in Spring MVC @RequestMapping methods:

@RequestMapping(value = &quot;/example1&quot;, method = RequestMethod.GET)
public String example1(Model model)
{
	model.addAttribute(&quot;currentUser&quot;, new User(&quot;foobar&quot;, &quot;Foo&quot;, &quot;BAR&quot;, &quot;&quot;));
	return &quot;/pages/example1&quot;;
}

 

IV Commons attributes

All the examples in this chapter can be found in the demo application on GitHub https://github.com/doanduyhai/ThymeLeafDemo, as example3.

In this chapter we’ll look into most commons attributes used in Thymeleaf.
 

A) th:if/th:unless

These attributes are quite straightforward. They are the mirrors of <c:if> and <c:otherwise> from JSTL.

&lt;div class=&quot;alert alert-error alert-block&quot; th:if=&quot;${error != null}&quot;&gt;
	&lt;h4 class=&quot;alert-heading&quot;&gt;Error!&lt;/h4&gt;
	 An unexpected error occurs, please contact the support team
&lt;/div&gt;
&lt;div class=&quot;alert alert-success alert-block&quot; th:unless=&quot;${error != null}&quot;&gt;
	&lt;h4 class=&quot;alert-heading&quot;&gt;Success!&lt;/h4&gt;
	 There is no error
&lt;/div&gt;		

B) th:switch/th:case

&lt;div th:switch=&quot;${switchCase}&quot;&gt; 
	&lt;p th:case=&quot;1&quot;&gt;Case 1&lt;/p&gt; 
	&lt;p th:case=&quot;2&quot;&gt;Case 2&lt;/p&gt;
	&lt;p th:case=&quot;3&quot;&gt;Case 3&lt;/p&gt;
	&lt;p th:case=&quot;4&quot;&gt;Case 4&lt;/p&gt;
&lt;/div&gt;

Not much to say, quite straightforward.

C) th:text

Thymeleaf replaces all the HTML content of the current tag by the value provided by th:text

...
	 &lt;span class=&quot;alert alert-success&quot; th:text=&quot;${realText}&quot;&gt;This is an unescaped sample text&lt;/span&gt;
...

Please note that the text string provided by th:text will be escaped (HTML encoded) by Thymeleaf. If you want to include HTML tags in the text string, use the th:utext instead to avoid escaping.

D) th:attr

This attribute purpose is to add generic HTML attributes to a tag. This proves to be usefull when used with HTML 5 data-* attributes for data storage & binding.

&lt;a href=&quot;#&quot; 
	data-url=&quot;http://www.google.com&quot;
	title=&quot;Link to Google&quot;
	th:attr=&quot;data-url=${dataUrl},title=${realTitle}&quot;
	th:text=&quot;${dataUrl}&quot;&gt;
	http://www.google.com
&lt;/a&gt;

At line 4 we define 2 attributes to be evaluated by the engine: data-url & title. The Thymeleaf th:attr accepts a list of HTML attributes separated by a coma. Please note that for the title value we could have defined it with the dedicated th:title as well, the choice is yours.

The result after rendering is:

<a title=”Link to ThymeLeaf” href=”#” data-url=”http://www.thymeleaf.org”>

Usually we use the th:attr to add special HTML 5 data-* attributes. For common HTML attributes we use the existing Thymeleaf dedicated one.

 

E) th:value

The th:value attribute is reserved for HTML data input tags like <input>, <option>

&lt;input class=&quot;input-medium&quot;
	type=&quot;text&quot;
	value=&quot;Name input&quot;
	th:value=&quot;${nameInput}&quot;&gt;
&lt;/input&gt;

F) th:each

This is the most usefull tag when you need to iterate through a collection of data.

&lt;table class=&quot;table table-bordered&quot;&gt;
	&lt;thead&gt;
		&lt;tr class=&quot;center middle&quot;&gt;
			&lt;th&gt;Name&lt;/th&gt;
			&lt;th&gt;Row count&lt;/th&gt;
			&lt;th&gt;Row index&lt;/th&gt;
			&lt;th&gt;List size&lt;/th&gt;
			&lt;th&gt;Even count&lt;/th&gt;
			&lt;th&gt;Odd count&lt;/th&gt;
		&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
		&lt;tr th:each=&quot;artist,rowStat : ${listArtits}&quot;&gt;
			&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.name}&quot;&gt;John Woo&lt;/td&gt;
			&lt;td class=&quot;center middle&quot; th:text=&quot;${rowStat.count}&quot;&gt;1&lt;/td&gt;
			&lt;td class=&quot;center middle&quot; th:text=&quot;${rowStat.index}&quot;&gt;0&lt;/td&gt;
			&lt;td class=&quot;center middle&quot; th:text=&quot;${rowStat.size}&quot;&gt;1&lt;/td&gt;
			&lt;td class=&quot;center middle&quot; th:text=&quot;${rowStat.even}&quot;&gt;false&lt;/td&gt;
			&lt;td class=&quot;center middle&quot; th:text=&quot;${rowStat.odd}&quot;&gt;true&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;

The result at runtime:
thymeleaf-exemple3-iteration

At line 13, we define the th:each attribute. The syntax is: rowVariable[,rowStatVariable] : ${collection}

The rowStatVariable is an optional variable to keep track of the iteration status. This variable exposes the following properties:

  • index: iteration index, starting at 0
  • count: iteration count, starting at 1
  • size: size of the list over which the iteration is done
  • current: current value of the iteration
  • even/odd: boolean to indicate whether the current element in the iteration is even or odd
  • first/last: boolean to indicate whether the current element is the first/last element of the iteration

We realize that we could also use the current property of the rowStatVariable variable to access the current value. Instead of th:text=”${artist.name}” we could use th:text=”${rowStat.current.name}”

Please note that in this example, artist is a local variable used for the iteration. Its scope is limited to the <tr> tag. Any attempt to access artist outside of its scope will result in error.

 

G) th:with/th:object

th:with defines a local variable for the current context (e.g. current HTML tag and its children).

&lt;div th:with=&quot;currentArtist=${listArtits[1]}&quot;&gt;
	&lt;span class=&quot;label label-important&quot; th:text=&quot;${currentArtist.name}&quot;&gt;Michael Jackson&lt;/span&gt;&lt;br/&gt;
	&lt;span class=&quot;label label-info&quot; th:text=&quot;${currentArtist.bio}&quot;&gt;Michael Joseph Jackson (August 29, 1958 - June 25, 2009) was an American recording artist, entertainer, and businessman...&lt;/span&gt;&lt;br/&gt;
	&lt;span class=&quot;label label-inverse&quot; th:text=&quot;${currentArtist.discography}&quot;&gt;Got to Be There, B...&lt;/span&gt;
&lt;/div&gt;

On the other hand th:object defines a selected object that can be accessed in the current context (e.g. current HTML tag and its children) by the special asterisk *{ } short-hand syntax.

The meaning of this attribute is similar to the With() syntax in C# language.

&lt;div th:object=&quot;${listArtits[0]}&quot;&gt;
	&lt;span class=&quot;label label-important&quot; th:text=&quot;*{name}&quot;&gt;Michael Jackson&lt;/span&gt;&lt;br/&gt;
	&lt;span class=&quot;label label-info&quot; th:text=&quot;*{bio}&quot;&gt;Michael Joseph Jackson (August 29, 1958 - June 25, 2009) was an American recording artist, entertainer, and businessman...&lt;/span&gt;&lt;br/&gt;
	&lt;span class=&quot;label label-inverse&quot; th:text=&quot;*{discography}&quot;&gt;Got to Be There, B...&lt;/span&gt;
&lt;/div&gt;

The main difference between th:object and th:with is that th:object defines a selected object in the current context accessible by the special *{ } syntax. All property look-up inside a *{ } expression is done with regard to the selected object. Inside the same context the standard syntax with ${ } can still be used though.

On the other hand th:with will create a new temporary variable in the variable map (Spring Model object) whose scope is limited to the current context.

 

H) th:remove

This attribute is usefull for template static preview. Let’s suppose that we have a template file with a table containing a list of artists. We will design the table using the th:each attribute as described previously to iterate through the result list.

However our page will look very poor in a static display since it only has one row. We could remedy to this by adding extra mocked rows but they need to be removed at runtime. That’s the purpose of th:remove

&lt;tbody&gt;
	&lt;tr th:each=&quot;artist,rowStat : ${listArtits}&quot;&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${rowStat.count}&quot;&gt;1&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.name}&quot;&gt;Michael Jackson&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.discography}&quot;&gt;Got to Be There, Ben, Music &amp;amp; Me, Forever Michael...&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.bio}&quot;&gt;Michael Joseph Jackson (August 29, 1958 - June 25, 2009) was an American recording artist, entertainer, and businessman...&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr th:remove=&quot;all&quot;&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${rowStat.count}&quot;&gt;2&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.name}&quot;&gt;Madonna&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.discography}&quot;&gt;Madonna, Like a Virgin, True Blue...&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.bio}&quot;&gt;Madonna (born Madonna Louise Ciccone August 16, 1958) is an American singer, songwriter, actress and entrepreneur...&lt;/td&gt;				
	&lt;/tr&gt;
	&lt;tr th:remove=&quot;all&quot;&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${rowStat.count}&quot;&gt;3&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.name}&quot;&gt;Elton John&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.discography}&quot;&gt;Empty Sky, Elton John, Tumbleweed Connection...&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.bio}&quot;&gt;Sir Elton Hercules John, CBE (born Reginald Kenneth Dwight on 25 March 1947) is an English rock singer-songwriter, composer, pianist and occasional actor....&lt;/td&gt;				
	&lt;/tr&gt;				
&lt;/tbody&gt;

The second and third row (line 8 & 14) will be removed at runtime by the engine. However if we have many mocked rows we should add the th:remove for each of them which is quite tedious.

The solution is given again by th:remove with the value “all-but-first” instead of “all“. It tells the engine to remove all the children of the current tag except the first one. The above code can be modified to take advantage of this feature.

&lt;tbody th:remove=&quot;all-but-first&quot;&gt;
	&lt;tr th:each=&quot;artist,rowStat : ${listArtits}&quot;&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${rowStat.count}&quot;&gt;1&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.name}&quot;&gt;Michael Jackson&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.discography}&quot;&gt;Got to Be There, Ben, Music &amp;amp; Me, Forever Michael...&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.bio}&quot;&gt;Michael Joseph Jackson (August 29, 1958 - June 25, 2009) was an American recording artist, entertainer, and businessman...&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${rowStat.count}&quot;&gt;2&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.name}&quot;&gt;Madonna&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.discography}&quot;&gt;Madonna, Like a Virgin, True Blue...&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.bio}&quot;&gt;Madonna (born Madonna Louise Ciccone August 16, 1958) is an American singer, songwriter, actress and entrepreneur...&lt;/td&gt;				
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${rowStat.count}&quot;&gt;3&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.name}&quot;&gt;Elton John&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.discography}&quot;&gt;Empty Sky, Elton John, Tumbleweed Connection...&lt;/td&gt;
		&lt;td class=&quot;center middle&quot; th:text=&quot;${artist.bio}&quot;&gt;Sir Elton Hercules John, CBE (born Reginald Kenneth Dwight on 25 March 1947) is an English rock singer-songwriter, composer, pianist and occasional actor....&lt;/td&gt;				
	&lt;/tr&gt;				
&lt;/tbody&gt;

The th:remove supports some more values, please refer to the documentation for the complete list.

To be continued…

22 Comments

  1. Pingback: Spring MVC part VI: ThymeLeaf advanced usage « Yet Another Java Blog

  2. loicg

    I ve recently changed my mind from using Play on GAE to using Spring 3 ( + extensions) integrated with Thymeleaf (still on GAE)…
    … Your post was very helpful i ve saved time ๐Ÿ™‚

    Reply
  3. hadi

    hi doan ๐Ÿ™
    i cant run the examples that on githup.please help me

    Reply
    1. DuyHai DOAN

      hello hadi

      what issues did you have ?

      Reply
  4. Pingback: Confluence: Union Bank of California

  5. gihrig

    I found this helpful, having worked with Thymeleaf a little, it’s nice to see the work of some one who knows his way around.

    I really appreciated the example app, it worked perfectly for me. I learned a few things about the differences between th:with and th:object. Also the section on th:remove was very helpful to me.

    Thanks

    Reply
  6. yngbldjr

    Love the post! Thanks for this. Thymeleaf needs to have some more documentation on the different utility methods it supports (Without reading the source code). Your posts have helped me in finding this info.

    Reply
    1. DuyHai DOAN

      You’re welcomed

      Reply
  7. Madhurika

    Hello,
    I am very new to java and i have to work with spring3+thymeleaf.
    Though your posts are useful but i am not able to completely understand .
    Can you please step by step give some tutorial as to how to develop a simple web application in eclipse using spring3 framework with thymeleaf.?
    Also what all jar files are needed for the same..!

    Reply
    1. DuyHai DOAN

      Hello Madhurika

      You can check my Github project : https://github.com/doanduyhai/ThymeLeafDemo

      All the source code to build a working Web Application with Thymeleaf is there

      Reply
      1. Madhurika

        thanks.:)

        Reply
  8. Madhurika

    Hello..
    I have to take one input and accordingly fetch my data from database and then have to display in the form of table in the same page using thymeleaf+spring3.
    I have tried alot but failed miserably..can you help.?

    Reply
  9. Malinda

    This blog, Solar Shades โ€œSpring MVC part III: ThymeLeaf integration | DuyHai’s Java Blogโ€ was amazing. Iโ€™m impressing out a replicate to show my associates. Thanks a lot-Amparo

    Reply
    1. doanduyhai

      Happy that it helps ๐Ÿ™‚

      Reply
  10. rue

    Thanks for the good introduction into thymeleaf, helping a lot to get started!

    BTW, wicket templates are also pure HTML, regarding your comment on TAL being the only other one you know.

    Reply
  11. Pingback: My second step with Thymeleaf (migrating JSPX generated by Spring Roo) | Trying things

  12. sarala rajagopalan

    Concise tutorial on Thymeleaf…thanks!

    Reply
  13. Vivek

    hi doanduyhai…
    i got your code from github, It’s Really Helpfull to me..

    Reply
  14. Lan Le-Dinh

    Hi Hai,

    I happened to discover your discussion here.

    I am interested in Web Development myself and would like to have contact with you to exchange ideas.

    Regards

    Lan

    Reply
  15. Good one! Thanks ๐Ÿ™‚

    Reply
  16. Ravshan Samandarov

    dialect for using data attributes: https://github.com/mxab/thymeleaf-extras-data-attribute

    Reply
  17. Raymond

    I’ve learn a few good stuff here. Certainly
    worth bookmarking for revisiting. I wonder how a lot effort
    you set to make any such fantastic informative site.

    Reply

Leave a Reply to DuyHai DOAN Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.