HomeEducationHow one can Iterate over a Record in Thymeleaf - TechBlogStation Acquire...

How one can Iterate over a Record in Thymeleaf – TechBlogStation Acquire US

The Record is probably the most generally used assortment in Java. We frequently must ship the objects in an inventory to a view template that shall be displayed within the Person Interface. Thymeleaf is a very fashionable template engine and Spring Boot supplies nice help to Thymeleaf. That is the explanation, Thymeleaf is used generally with Spring Boot purposes.

On this brief tutorial collection, we’re taking a look at varied points of Thymeleaf. In considered one of our earlier articles, we defined learn how to iterate over a Map in Thymeleaf, and right here we’re taking a look at learn how to loop by Record in Thymeleaf.

If you’re a Spring Boot developer or you might be simply beginning to be taught, it could be a good suggestion to undergo the full information of Thymeleaf with Spring Boot.

Passing a Record to Thymeleaf

Let’s contemplate a easy instance first, so we’ve got our checklist of fruits that we wish to go to the Thymeleaf template and later show within the view.

The code snipped beneath exhibits learn how to go a fundamental checklist of strings in Thymeleaf.

Record<String> fruits = new ArrayList<>();



Now, contemplate an instance the place we wish to go a Record of Objects to the Thymeleaf. Right here, we’re passing Record of Worker to the Thymeleaf template.

// beneath service.getAll() technique is returning Record of Worker

Record<Worker> staff = service.getAll();
mannequin.addAttribute("staff", staff);

Within the beneath part, we’ll take a look at how we iterate over each checklist staff and fruits.

Loop / Iterate by Record in Thymeleaf

In our Thymeleaf template, now we’ve got one workers and a fruits checklist. Let’s learn to iterate over the checklist in Thymeleaf.

First, we’ll loop by our easy checklist of fruits:

	<li th:every="fruit: $fruits"><span th:textual content="$fruit"></span></li>

It’s easy, we’re utilizing th:every to iterate over an inventory. Every merchandise from fruits shall be assigned to variable fruit one after the other as iteration goes on. The iteration will occur n instances the place n is the dimensions of checklist.

Now Let’s iterate Staff and print the Worker object’s values:

<desk model="border: stable 1px black;">
		<tr th:every="worker: $staff">
			<td th:textual content="$" />
			<td th:textual content="$worker.title" />

Within the above instance, we’re contemplating worker has two states id, and title. The code is fairly easy and self-explanatory. We’re utilizing th:every for iteration then we’re accessing id, and title from the fetched worker.


On this tutorial, we’ve got discovered learn how to iterate over a Record in Thyemeleaf utilizing two easy examples.

Thanks for Studying!

#Iterate #Record #Thymeleaf #TechBlogStation

Continue to the category


Please enter your comment!
Please enter your name here

- Advertisment -spot_img

Most Popular

Recent Comments