Expression Language in Thymeleaf

In my previous tutorial I have explained about the simple hello world example to start writing your first example using the Thymeleaf framework. If you are not familiar with this new framework, I would recommend to read that tutorial before start reading this tutorial. When we work with web frameworks, one of the important feature is the simplification of using the expression languages. This tutorial explains the various syntax used by the Thymeleaf framework for accessing the values using its expression syntax. Look at the below list of various types of expression syntax supported by this framework.

  • Simple expressions
    • Variable Expressions: ${…}
    • Selection Variable Expressions: *{…}
    • Message Expressions: #{…}
    • Link URL Expressions: @{…}
  • Literals
    • Text literals: ‘one text’, ‘Another one!’,…
    • Number literals: 0, 34, 3.0, 12.3,…
    • Boolean literals: true, false
    • Null literal: null
    • Literal tokens: one, sometext, main,…
  • Text operations
    • String concatenation: +
    • Literal substitutions: |The name is ${name}|
  • Arithmetic operations
    • Binary operators: +, -, *, /, %
    • Minus sign (unary operator): -
  • Boolean operations
    • Binary operators: and, or
    • Boolean negation (unary operator): !, not
  • Comparisons and equality
    • Comparators: >, <, >=, <= (gt, lt, ge, le)
    • Equality operators: ==, != (eq, ne)
  • Conditional operators
    • If-then: (if) ? (then)
    • If-then-else: (if) ? (then) : (else)
    • Default: (value) ?: (defaultvalue)

Among the above list, first one is the important to understand.

1. Variable Expression

Variable Expressions and Selection Variable Expressions work similar except that later will work different when used with seletecd objects. Look at the below example.

<div th:object="${obj.userObj}">
    <p>Name: <span th:text="*{firstName}">Krishna</span>.</p>
    <p>Surname: <span th:text="*{lastName}">Srinivasan</span>.</p>
    <p>Nationality: <span th:text="*{country}">India</span>.</p>
  </div>

Which is equivalent to

<div>
    <p>Name: <span th:text="*{obj.userObj.firstName}">Krishna</span>.</p>
    <p>Surname: <span th:text="*{obj.userObj.lastName}">Srinivasan</span>.</p>
    <p>Nationality: <span th:text="*{obj.userObj.country}">India</span>.</p>
  </div>

We can use dollar and asterisk syntax together. From the above example, you could notice that instead of asterisk, you can even use the dollar syntax.

2. Message Expression

It is very simple to access a message from a resource bundles.

<p th:text="#{hello}">Hello World Thymeleaf Offline!!</p>

The above “hello” key is taken from message properties file. Also you can use the dynamic values in the messages.

hello=Welcome {0}, Hello World Thymeleaf!!

For the above property configuration, you can pass the parameter as below

<p th:text="#{hello(${userObj.userName})}}">Hello World Thymeleaf Offline!!</p>

3. Link Expressions

Thymeleaf Standard Dialect has a special syntax for the links, the @ syntax: @{…}. Thymeleaf can handle absolute URLs like http://www.thymeleaf.org can handle at any situation. But for the relative URLs, we have specify the context path. th:href is used for setting the href attribute of the link element. Look at the example snippet below.

Absolute URL

<a href="user.html" th:href="@{http://localhost:8080/user/details(userId=${o.id})}">View User</a>

Relative URL

In the case of absolute URL, context path is added to the final URL.

<a href="user.html" th:href="@{/user/details(userId=${o.id})}">View User</a>

I hope this article provided basic idea of how to use the expression language in Thymeleaf. Other types of expressions are not new and easy to use, so I am not explaining in this tutorial. If you have any clarification, please write it in the comments section.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*