• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Expression Language in Thymeleaf

November 26, 2013 //  by Krishna Srinivasan//  Leave a Comment

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.

  • Expression Language in JSP 2.0
  • Spring Expression Language

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.

Category: Web FrameworksTag: expression language, Thymeleaf

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.

Previous Post: « Top 5 Best Free Online CSS3 Tools For Web Design
Next Post: Context Objects in Thymeleaf »

Reader Interactions

Leave a Reply 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.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact