gpt4 book ai didi

java - thymeleaf th :each filtered with th:if

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:11:50 24 4
gpt4 key购买 nike

我需要迭代并创建 <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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com