Thymeleaf th:每个都在元素之间加入昏迷

yam*_*ami 9 java spring thymeleaf

我有收藏 X

我遍历它并像这样写:

<span th:each="a, stat : ${X}"
      th:text="${X[__${stat.index}__].someProperty} + ','">
</span>
Run Code Online (Sandbox Code Playgroud)

我的另一个尝试是:

<span th:each="a, stat : ${X}" th:for="|a${stat.index}|"
      th:text="${X[__${stat.index}__].someProperty} + ','">
</span>
Run Code Online (Sandbox Code Playgroud)

不幸的是输出是一样的.

跨度中的输出是:

test1, test2, test3,
Run Code Online (Sandbox Code Playgroud)

我希望输出为:

test1, test2, test3
Run Code Online (Sandbox Code Playgroud)

最后没有逗号.我怎样才能做到这一点?

解:

  1. 注意th:text与元素类型关联的属性值span 不得包含"<"字符.

码:

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

ndr*_*one 20

Thymeleaf有一个迭代属性,last请参阅此处的文档:http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#keeping-iteration-status

使用

<span th:each="a, iterStat : ${X}" th:text="!${iterStat.last} ? ${a} + ',': ${a}"></span>
Run Code Online (Sandbox Code Playgroud)

  • 我的 Intellij 想法将此语法突出显示为错误,因此我将其更改为这样并且有效: th:text="${!iterStat.last} ? ${a} + ',': ${a}" (2认同)