Thymeleaf th:each adding coma between elements

Viewed 16983

I have collection X

I iterate through it and write it like that:

<span th:each="a, stat : ${X}"
      th:text="${X[__${stat.index}__].someProperty} + ','">
</span>

my other try was:

<span th:each="a, stat : ${X}" th:for="|a${stat.index}|"
      th:text="${X[__${stat.index}__].someProperty} + ','">
</span>

unfortunately the output is the same.

the output in the span is:

test1, test2, test3,

I want the output to be:

test1, test2, test3

without the comma at the end. How can I achieve that?

Solution:

  1. Beware of The value of attribute th:text associated with an element type span must not contain the '<' character.

Code:

<span th:each="a, stat : ${X}"
      th:text=" ${X[__${stat.index}__].someProperty} +  (${stat.size-1 > stat.index}? ',':'') ">
</span>
1 Answers
Related