Thymeleaf th:每个用th过滤:if

Far*_*ook 9 java spring spring-mvc thymeleaf spring-boot

我需要迭代,并创建<span>针对每个元件componentcomponents,其具有阵列name'MATERIAL'

我的代码如下

<span th:each="component : ${body.components}" 
      th:object="${component}">
        <button th:if="*{name} == 'MATERIAL'" 
                th:text="*{title}"></button>
</span>
Run Code Online (Sandbox Code Playgroud)

这段代码一切正常,直到它产生一组空<span>元素,如果name它不相等'MATERIAL'.我不希望这个空<span>元素被创建.

我也试过以下

<span th:each="component : ${body.components}" 
      th:object="${component}"
      th:if="*{name} == 'MATERIAL'">
         <button th:text="*{title}"></button>
</span>
Run Code Online (Sandbox Code Playgroud)

这导致空输出并且根本不打印任何内容.有人可以帮我这个.

tma*_*wen 15

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

<span th:each="component : ${body.components}" 
      th:object="${component}"
      th:if="${component.name} == 'MATERIAL'">
  <button th:text="*{title}"></button>
</span>
Run Code Online (Sandbox Code Playgroud)