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

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

    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 […]

  • Digital Products

    Mastering Legacy Application Modernization: Strategies for Success

    The ironic truth of the business world is that change is the only constant. But this means that failing to keep pace with the competition and its technologies will only end with you falling behind. That’s where legacy application modernization enters the fold. When you modernize legacy applications, your team gains access to new features […]

  • Digital Products

    CTO Confessions Podcast

    In this episode of CTO Confessions, Rob Phillips, the Vice President of Software Engineering at UDig, digs into his journey from a passionate technologist in his youth to a seasoned leader in the tech industry. He shares valuable lessons on transitioning to senior leadership, the importance of understanding and articulating company problems, and the art of empowering teams for high performance.

  • Digital Products

    Navigating the Challenges of On Premise to Cloud Migration

    In today’s rapidly evolving technological landscape, the shift from on premise solutions to cloud-based infrastructure has become a pivotal transformation for organizations seeking to modernize their IT operations. This transition holds the promise of increased agility, cost savings, and enhanced scalability. However, it is not without its set of formidable challenges that organizations must navigate. […]

  • Digital Products

    The Power of Transferrable Skills in Tech Projects

    Every project has its own unique elements that require flexibility to be effective and achieve success. This often requires picking up new pieces of a tech stack, learning a new programming language, or a new project methodology. Fortunately, there are also many transferrable skills that carry over from one project to the next. In my […]

  • Digital Products

    The Four Pillars of Effective Digital Product Development

    In 2020 alone, approximately two billion consumers purchased at least one digital product. From software licenses to mobile apps and tech tools, consumers are becoming increasingly active in the digital product market, a trend that has naturally spurred brands across a wide range of industries to reevaluate their digital product design and development process workflows. […]