为什么不工作

时间:2019-02-11 11:38:57

标签: spring spring-boot thymeleaf

我正在尝试遍历包含对象列表的列表,即List我想知道为什么这不起作用,尝试使用“ i”,但是没有运气。

List<Object[]> lists;  // logic
model.addObject("lists", lists);
model.addObject("table_width", lists.get(0).length);

胸腺代码段

<table class="table table-responsive table-stripped table-collapsed table-bordered">
                <tr th:each="rows,rowStat : ${lists}">
                    <td th:text="${rowStat.count}"></td>

                    <td th:each="i : ${#numbers.sequence(0, table_width)}" th:text="${rows[${i}]}"></td>
                </tr>
</table>

3 个答案:

答案 0 :(得分:0)

我找到了办法,

<td th:each="i : ${#numbers.sequence(0, table_width-1)}" th:text="${rows[__${i}__]}"></td>

这可以解决问题

答案 1 :(得分:0)

您可以简单地遍历两个列表。无需使用const char test[] = "hello word"; char* str = malloc(sizeof test); memcpy(str, test, sizeof test); puts(str); bool ok = append_ch(&str, 'l', 9); if(!ok) asm ("HCF"); // error handling here puts(str); free(str); 助手。

#numbers

答案 2 :(得分:-1)

如果要迭代对象的集合(列表),请尝试以下示例:

HTML:

<div th:if="${not #lists.isEmpty(counts)}">
    <h2>Counts List</h2>
    <table class="table table-striped">
        <tr>
            <th>Id</th>
            <th>Name</th>
        </tr>
        <tr th:each="count : ${counts}">
            <td th:text="${count.id}"></td>
            <td th:text="${count.name}"></td>
        </tr>
    </table>
</div>

Java:

public List<Count> listAll() {
    List<Count> counts = new ArrayList<>();
    countRepository.findAll().forEach(counts::add);
    return counts;
}

在Thymeleaf文档-Iteration Basics部分中了解更多信息。