TAG Panel: Differentiate Your Customer Experience
Join the CX and Product Management Societies to hear from our panel of Human-Centered Design experts on the business value of Agentic AI.
This site uses cookies to enhance your browsing experience and deliver personalized content. By continuing to use this site, you consent to our use of cookies.
COOKIE POLICY
Hey guys, I’m back again with a few more tips to help you navigate the waters of Spring Thymeleaf. Part one of this series can be found here.
In this article, we’re going to go over a few intermediate pieces of Thymeleaf’s functionality.
There are several helper objects available to use in templates. They are:
If you’re using Java 8’s newer time classes, you will want to include an extra standard template library in your maven file. It contains the #temporals expression set for handling classes like LocalDate and LocalDateTime.
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
Using th:inline and a little notation, you can inject anything accessible to Thymeleaf into your JavaScript. We usually use this to create a data object that’s accessible on the global scope for referencing your data objects on page load without an ajax call. I use a namespace with a simple getter and setter running on Lodash to add a little insulation around the global objects.
Using our people example from part 1:
Our JavaScript that runs the getter and setter would look like this:
And Usage of that function would look like this:
You can reference static classes directly in your templates (or inline scripts!) without putting them on the model by calling the full package name. The example below uses this syntax to access an Enum for a comparison operation.
That’s all for today. In the last part of this series, we will cover a few advanced operations including fragments, projection and selection. See you next time!