首页 > 解决方案 > 在 Thymeleaf 片段中传递数组作为参数

问题描述

是否可以将数组传递给这样的片段:

<div th:replace="fragments :: test_fragment( {'one', 'two', 'three'} )"></div>

并像这样在片段中迭代:

<div th:fragment="test_fragment(numberArray)">

    <span th:each="number : ${numberArray}" th:text="${number}"></span>

</div>

作为奖励,多维数组也可以吗?

我在 Spring Boot 2.0 项目中使用 Thymeleaf。

标签: spring-bootthymeleaf

解决方案


对的,这是可能的。下面的代码应该可以解决问题。唯一的区别是${}在数组之外添加了 ,。

<div th:replace="fragments :: test_fragment(${ {'one', 'two', 'three'} })"></div>

推荐阅读