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

  • Digital Products

    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.

  • Digital Products

    The Bloated SaaS Era: Paying More for Less While Businesses Wait

    SaaS was supposed to make business faster, smarter, and more efficient. Instead, it’s become bloated, expensive, and painfully slow to change. The platforms we rely on—Salesforce, Workday, SAP, and others—haven’t truly innovated in years. Yet, they demand massive investments in re-implementation, process re-engineering, and data migration just to keep up. It’s time to ask: Are […]

  • Digital Products

    Reid Braswell Joins UDig as Vice President, Software Engineering

    UDig is proud to welcome Reid Braswell as our new Vice President of Software Engineering. With over 13 years of experience in technology consulting, Reid brings deep expertise in digital transformation, modern software engineering, and client-focused solutions. His leadership and passion for solving complex challenges make him an exceptional addition to the UDig team. Reid’s […]

  • Digital Products

    Energy 2025 – Expansion of Fossil Fuels or Carbon Reduction?

    Now that the election is behind us, we have an opportunity to anticipate the possible effects on the energy industry under this new administration. What strategies will be impacted? What will remain the same? What opportunities can we take advantage of in 2025? This blog is meant to dig into these questions and provide some […]

  • Digital Products

    The Growing Importance of Digital Accessibility

    Embracing Digital Accessibility: A Moral and Business Imperative In today’s digital landscape, accessibility has become crucial for businesses and organizations. With increasing awareness and legal requirements, ensuring that digital products are accessible to all users, including those with disabilities, is not just a compliance issue but a moral imperative. At UDig, we champion ADA compliance […]

  • Digital Products

    Unlocking Business Potential: The Power of Custom Application Development

    Like any savvy business leader, you’re likely always on the lookout for tools to give your company a competitive edge. And in doing so, you’ve undoubtedly considered investing in custom application development. But the question is, how do you ensure that such a major investment in a custom web application development provides a strong return on […]