java - thymeleaf th :each filtered with th:if

标签 java spring spring-mvc spring-boot thymeleaf

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

相关文章:

java - Android Studio - 将用户信息保存在 CSV 文件中并读取

java - Hibernate validator 总是说集合为空

java - Spark 重用数据集的持久化功能

java - DLL 实现&无法产生静态引用错误

java - InternalResourceViewResolver 未解析

java - 尽管允许,但未经授权的请求

spring - Maven Jetty 运行 JNDI 配置

java - 如何模拟 Spring SecurityContext 以便我可以将它与 TestNG 一起使用?

java - 正确(并且轻松?)启动 Spring MVC 项目

java - 将 Spring MVC 与 html 结合使用