java Thymeleaf th:每个元素之间添加昏迷

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36087491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:56:52  来源:igfitidea点击:

Thymeleaf th:each adding coma between elements

javaspringthymeleaf

提问by Kamil Witkowski

I have collection X

我有收藏 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:textassociated with an element type spanmust not contain the '<' character.
  1. 注意th:text与元素类型关联的属性值span不得包含“<”字符

Code:

代码:

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

回答by ndrone

Thymeleaf has an iteration property lastsee the documentation here: http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#keeping-iteration-status

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

use

利用

<span th:each="a, iterStat : ${X}" th:text="!${iterStat.last} ? ${a} + ',': ${a}"></span>