Iterating a list in Thymeleaf

I started Apache tiles in 2010. It is convenient for the tasks I am doing. Thymeleaf picked up in parallel. By the time I read Thymeleaf template engine is trying to replace JSP, I left it as such and continue using Tiles 🙂

Anyway I wanted to see what is it. I was trying to iterate a list using th: namespace.

I have a controller like this, which returns a list of my entity.

[code language=”java”]

@GetMapping (value = “/”)
public String index(Locale locale, Model model) {
RestTemplate restTemplate = new RestTemplate();
List fromRest = restTemplate.getForObject(“http://localhost:8081/api/v1/feeditems”, List.class);
model.addAttribute(“feeditems”, (List)fromRest);
return “index”;
}

[/code]

And it is

[code language=”java”]

<div th:if=”${#lists.isEmpty(feeditems)}”>
Empty
</div>
<div th:if=”not ${#lists.isEmpty(feeditems)}”>
Not Empty
</div>

<div th:each=”post:${feeditems}”>
<div th:text=”${post.title}”></div>
</div>

[/code]

Here is the output:

Iterate through thymeleaf

I captured this the on the way to Kandy in Sri Lanka
I captured this the on the way to Kandy in Sri Lanka

Leave a Comment

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

Scroll to Top