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 3

Spring Thymeleaf Tips | Part 3
Back to insights

Thymeleaf Fragments, Projection and Selection

I’m back again with the third (and final) installment of our exploration of some of the finer points of Spring’s Thymleaf templating system. Today we’re going to go over a couple of complex topics in more detail. Fragments give you the option to reuse a template with a th:replace tag, selection lets you filter a collection on a set of conditions and projection fattens out a collection of objects into a consumable string result.

Fragments are Your Friend

Fragments grant you a ton of power and help you create modularized components that can be reused all over your web app. The basic idea here is that you define a fragment and then you can reference that template using a th:replace anywhere in your application. Let’s take the table from Parts 1 & 2 and turn it into a fragment.

When we last left it, the body of our index.html looked like this:

Spring Thymeleaf Tips | Part 3 - photo 1

To make a fragment, we create a new file (ours will be in a fragments folder, but that’s not necessary). We’re calling this file common_fragments.html.

Spring Thymeleaf Tips | Part 3 - photo 2

We then use the th:fragment notation to name our table fragment.

Spring Thymeleaf Tips | Part 3 - photo 3

The argument specifies the name of the object we are passing to the fragment. In this case, we’ve named it the same as in our core model – people. Next we add the table code.

Spring Thymeleaf Tips | Part 3 - photo 4

And finally, we reference the fragment in our index.html using th:replace and passing in our model object:

Spring Thymeleaf Tips | Part 3 - photo 5

Spring Thymeleaf Tips | Part 3 - photo 6

And just like that, we’ve got a reusable piece of template that we can throw in with a single line whenever we need it.

Use Projection with Th:each to Flatten a Collection

Suppose we wanted to simplify our list down to only name and row count. We could use the projection notation to simplify this collection as follows:

Spring Thymeleaf Tips | Part 3 - photo 7

Spring Thymeleaf Tips | Part 3 - photo 8

 

It’s important to note that you can’t access other properties you didn’t reference up top after you run through projection. The object is flattened out into a simple string in the example above.

For the remaining examples, I’ve added the addresses back in so we have more data to work with.

Spring Thymeleaf Tips | Part 3 - photo 9

Spring Thymeleaf Tips | Part 3 - photo 10

 

Use the Selection with Th:each to Filter a Collection

Selection operations allow us to filter a collection on a given predicate specified in the th:each declaration. The notation looks like this:

Spring Thymeleaf Tips | Part 3 - photo 11

The resulting table has the non-matching entry filtered out:

Spring Thymeleaf Tips | Part 3 - photo 12

So let’s say we wanted to break this data into tables by state. We could use a combination of the concepts in this series to do it with minimal code.

First, I modify the people table fragment to take a state code:

Spring Thymeleaf Tips | Part 3 - photo 13

Next, we will add a list of state codes into my model on the controller

Spring Thymeleaf Tips | Part 3 - photo 14

Finally, we wrap the fragment call in index in a th:each for each code we provided in the service:

Spring Thymeleaf Tips | Part 3 - photo 15

When we load the page we see:

Spring Thymeleaf Tips | Part 3 - photo 16

Success!

That’s it for our series on Thymeleaf. Hopefully, you learned something new through our sampling of Thymeleaf’s feature set.  Thanks for reading!

Digging In

  • Digital Products

    What to Ask in a UX Discovery Session

    Better questions lead to better design. In my years as a UI/UX designer, I’ve learned that many design challenges may come from misalignment at the start. Discovery sessions are where product clarity is either made or missed. These are a few discovery questions I consistently return to in the discovery phase of my UX design […]

  • Digital Products

    Designing For Play and Friction in a Fast-Paced World

    At UDig, play is an integral part of our philosophy. “Having fun” is embedded in our culture, and we always find opportunities to use play to encourage collaboration, ignite creativity, and make room for bold experimentation to build stronger teams and solve problems ranging from the seemingly simple to the most complex. I always have […]

  • Digital Products

    Config 2025 Day Two Recap

    It felt as though Config 2025 ended as soon as it began, and I believe those of us that attended are all the better for it. By the end of the day, various inspirational and informative talks had been given by thought leaders and innovators in the product space. Between sessions, we had the opportunity […]

  • Digital Products

    Inside Config 2025: What’s New in Figma

    Config 2025 kicked off with a bang on Day 1. Figma’s annual conference brings together designers, developers, and all those involved in the making of a product. The 2-day event has a stacked lineup of accomplished speakers ready to share their insights on the world of product building. At today’s opening keynote, the Figma team, […]

  • Digital Products

    Choosing the Right Modernization Approach

    When organizations decide it’s time to modernize their technology infrastructure, choosing the right approach is crucial. Modernization isn’t merely a technical upgrade; it’s a strategic business move that significantly impacts growth, agility, and long-term success. Here’s how your company can effectively begin to select the best modernization strategy tailored to your goals and challenges. 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.