Your Privacy

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

Skip to main content

Spring Thymeleaf Tips | Part 2

Spring Thymeleaf Tips | Part 2
Back to insights

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.

Thymeleaf Standard Expressions

There are several helper objects available to use in templates. They are:

  • #dates: utility methods for util.Date objects: formatting, component extraction, etc.
  • #calendars: analogous to #dates, but for util.Calendar objects
  • #numbers: utility methods for formatting numeric objects
  • #strings: utility methods for String objects: contains, startsWith, prepending/appending, etc.
  • #objects: utility methods for objects in general
  • #bools: utility methods for Boolean evaluation
  • #arrays: utility methods for arrays
  • #lists: utility methods for lists
  • #sets: utility methods for sets
  • #maps: utility methods for maps
  • #aggregates: utility methods for creating aggregates on arrays or collections
  • #messages: utility methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax
  • #ids: utility methods for dealing with id attributes that might be repeated (for example, as a result of an iteration)

Import Temporals Expressions To Handle Java 8 Time

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>

Inject Objects Directly Into Your JavaScript

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:

Reference Static Classes

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!

Digging In

  • Software Engineering

    When There’s Too Much to Fix: How Smart Prioritization Unlocks Revenue at Scale

    Every operations team has a backlog. The question isn’t whether you can clear it — it’s whether you’re clearing it in the right order. For most teams, the honest answer is no. And that gap between the order work gets done, and the order it should get done is quietly costing organizations millions. The Volume Problem High-volume exception processing shows up across […]

  • Software Engineering

    Creating Reusable Code Templates to Reduce Client Project Startup Time

    In consulting, one of the least visible but most expensive phases of a project is the beginning. Teams can spend days or weeks setting up repositories, agreeing on structure, wiring basic infrastructure, and solving problems that have already been solved many times before. Code templates are a practical way to reduce overhead while improving consistency. […]

  • Software Engineering

    Player Three Has Entered the Game: How AI Is Finally Bridging the Divide Between Design and Engineering

    As AI begins to become more prominent in our day-to-day lives, I find myself in a unique position. As a practicing software engineer and UI/UX designer, I am genuinely happy to see the introduction of AI tools begin to take shape in our industry. But more importantly, I am happy to start seeing the effects it is having on what has historically been a pretty challenging relationship: the […]

  • Software Engineering

    The Disappearing Middle of Software Work: Why the Bookends – Strategy & Impact – Matter Most Now

    Here’s a question nobody in enterprise software wants to sit with: what happens to the middle? Not the middle of the org chart. The middle of the work. The vast, expensive layer of effort that has defined enterprise software delivery for thirty years—translating what the business wants into working code. The requirements-to-implementation pipeline. The “build phase.” […]

  • Software Engineering

    Zero-Code Telemetry with OpenTelemetry’s OBI

    Full distributed tracing and exception capture for any application — without writing a single line of instrumentation code. View the source code on GitHub → The Premise Observability is essential for understanding what’s happening inside your services, but instrumenting an application by hand — adding trace spans, logging calls, and metric counters throughout your codebase […]

  • Software Engineering

    Building a Consultant in the Trenches: How Playing Offensive Line Shaped My Consulting Career

    People often ask me the same question when they find out that I played college football: “Do you miss it?” On the surface, it’s a bad question with an obvious answer. Yes. However, if I give myself a minute to sit with that question, the answer is more nuanced. Yes, I miss playing football, but […]