Search code examples
javaspringjspjakarta-eethymeleaf

Using Iteration variable in thymeleaf


Hi I am new to thymeleaf and am converting ma old project from jsp to thymeleaf.I am trying to convert a piece of code written in jsp which is :

<logic:iterate id="someForm" name="formName" property="nameList" indexId="i">
<%if (i%2==0)
{
 className="even";
 }
 else
 {
  className="odd";
 }
 %>
//some code here

can anyone help me with converting this code in thymeleaf ??


Solution

  • What you're looking for is in the Thymeleaf documentation. Assuming you need to iterate over your collection to display div tags:

    <div th:each="propName,iterStat : ${propNames}" th:class="${iterStat.odd}? 'odd' : 'even'">
        ...
    </div