Thymeleaf - 如何按索引循环列表 [英] Thymeleaf - How to loop a list by index

查看:44
本文介绍了Thymeleaf - 如何按索引循环列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何按索引循环?

Foo.java

public Foo {
    private List<String> tasks;
    ...
}

index.html

<p>Tasks:
    <span th:each="${index: #numbers.sequence(0, ${foo.tasks.length})}">
        <span th:text="${foo.tasks[index]}"></span>
    </span>
</p>

解析错误

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as each: "${index: #numbers.sequence(0,  ${student.tasks.length})}"

推荐答案

Thymeleaf th:each 允许你声明一个迭代状态变量

Thymeleaf th:each allows you to declare an iteration status variable

<span th:each="task,iter : ${foo.tasks}">

然后在循环中可以参考iter.indexiter.size.

Then in the loop you can refer to iter.index and iter.size.

参见 教程:使用 Thymeleaf - 6.2 保持迭代状态.

这篇关于Thymeleaf - 如何按索引循环列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆