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

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