Which Thymeleaf attribute is used to iterate over a collection?

Which Thymeleaf attribute is used to iterate over a collection?

A) th:each
B) th:for
C) th:loop
D) th:repeat

Answer:

A) th:each

Explanation:

The th:each attribute in Thymeleaf is used to iterate over a collection of items. It works similarly to a for-each loop in Java, allowing you to render a list of elements dynamically.

For example:


<ul>
    <li th:each="item : ${items}">[[${item.name}]]</li>
</ul>

In this example, th:each iterates over the items collection and renders each item as a list element.

Reference links:

https://www.javaguides.net/p/thymeleaf-tutorial.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top