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 1

Spring Thymeleaf Tips | Part 1
Back to insights

We’ve been developing a UI using Spring Thymeleaf (2.1.2) on our team. I’ve noticed a serious dearth of examples outside of specific questions on Stack Overflow, so I figured I’d throw my hat in the ring and offer a few tips and tricks we’ve picked up over the 10 or so months working with this templating system. This is part one of a series. In each exploration, we will examine a few different pieces of Thymeleaf that could make your life a little easier. This first one is a few tips and tricks that should help keep your templates a little more readable.

Make Function Calls Into Your POJOs

Sometimes we need a little more power behind our retrieval operations. When it’s too complicated to try and manage it inline or Thymeleaf’s expressions don’t cover your use case, you can always use a formatter method on your domain object and call that directly in the template.

Let’s say we have a list of people with addresses, and we want to format the second address field on its own line if it’s there. We could use a ternary operator for that in a big messy concatenation, but for the sake of readability, it’s easier to wrap it in a function and let the model serve up the formatted text.

This markup produces:

Use Ternary Operators for Unreliable Info

Sometimes you run across a case where a piece of data isn’t present yet in the state of your application. In some of these cases, clever use of the ternary operator can make for an inline data swap instead of having to use th:if or th:block.

Use RowStat to Grab the Index Th:Each

There’s a secondary argument available to th:each – rowStat. It allows you to access certain values around the collection you are iterating over.

Use Th:Field for Fun and Profit

If you don’t want to type the name of your input over and over again, use th:field. It turns this:

into this:

Use Th:Object for Cleaner Templates

For a specific block of code, you can use th:object to allow direct reference to an object’s members. Once we do that to our table, it looks like this:

Note the change in the notation from ‘$’ to ‘*’. This tells Thymeleaf to look up the tree for a parent object.

That’s all for today. Next time we’ll cover standard expression libraries, Java 8 time expressions, data types and integrating Thymeleaf service objects with your javascript.

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