thymeleaf 分割字符串遍历数组
时间: 2023-11-01 18:56:06 浏览: 39
您可以使用Thymeleaf的内置函数split来分割字符串,然后使用th:each循环来遍历数组。下面是一个示例代码:
```html
<div th:with="str='apple,banana,orange'">
<ul>
<li th:each="item : ${#strings.arraySplit(str, ',')}">
<span th:text="${item}"></span>
</li>
</ul>
</div>
```
在这个例子中,我们首先定义了一个字符串变量str,它的值是"apple,banana,orange"。然后使用Thymeleaf的内置函数#strings.arraySplit来将字符串按照逗号分割成数组。接着使用th:each循环遍历数组,将每个元素显示在列表中。
相关问题
thymeleaf遍历对象属性的数组
可以使用Thymeleaf的th:each指令来遍历对象属性的数组。假设有一个叫做"student"的对象,其中有一个属性叫做"courses",它是一个包含多个课程的数组,可以使用以下代码来遍历它:
```
<table>
<tbody>
<tr th:each="course : ${student.courses}">
<td th:text="${course.name}"></td>
<td th:text="${course.teacher}"></td>
<td th:text="${course.grade}"></td>
</tr>
</tbody>
</table>
```
在上面的代码中,th:each指令用于迭代"student.courses"数组,将每个数组项赋值给变量"course"。然后,在每次迭代时,使用th:text指令来显示每个课程的名称、老师和成绩。
Thymeleaf遍历图片数组
Thymeleaf遍历图片数组的代码示例:
```html
<div th:each="image : ${images}">
<img th:src="${image}" />
</div>
```
其中,`${images}`是一个存储图片路径的数组。在遍历数组时,使用Thymeleaf的`th:each`指令,将每个图片路径赋值给变量`image`,然后在`<img>`标签中使用`th:src`指令将图片路径作为`src`属性的值。这样就可以将数组中的所有图片展示出来了。