我需要迭代并创建 <span>
每个 component
的元素在components
具有 name
的数组的 'MATERIAL'
我的代码如下
<span th:each="component : ${body.components}"
th:object="${component}">
<button th:if="*{name} == 'MATERIAL'"
th:text="*{title}"></button>
</span>
此代码在生成一组空 <span>
之前一切正常元素如果 name
不等于 'MATERIAL'
.我不想要这个空的 <span>
要创建的元素。
我也试过下面的
<span th:each="component : ${body.components}"
th:object="${component}"
th:if="*{name} == 'MATERIAL'">
<button th:text="*{title}"></button>
</span>
这导致空输出并且根本不打印任何内容。有人可以帮我解决这个问题吗?
最佳答案
您应该使用点 (.) 直接引用迭代项属性,而不是在您的 html 元素:
<span th:each="component : ${body.components}"
th:object="${component}"
th:if="${component.name} == 'MATERIAL'">
<button th:text="*{title}"></button>
</span>
关于java - thymeleaf th :each filtered with th:if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29588391/