提问者:小点点

百里香叶th:每片用th:if过滤


我需要迭代并创建

我的代码如下

<span th:each="component : ${body.components}" 
      th:object="${component}">
        <button th:if="*{name} == 'MATERIAL'" 
                th:text="*{title}"></button>
</span>

这段代码工作得很好,直到它产生一组空

我也试了下面的

<span th:each="component : ${body.components}" 
      th:object="${component}"
      th:if="*{name} == 'MATERIAL'">
         <button th:text="*{title}"></button>
</span>

这会导致空输出,并且根本不打印任何内容。有人能帮我一下吗?


共1个答案

匿名用户

您应该直接使用点(.)符号引用迭代项属性,而不是在html元素中通过SpEL表达式计算(*{name}):

<span th:each="component : ${body.components}" 
      th:object="${component}"
      th:if="${component.name} == 'MATERIAL'">
  <button th:text="*{title}"></button>
</span>