首页 > 解决方案 > 错误的 Thymeleaf 迭代

问题描述

我不知道为什么,但是当我用 thymeleaf 迭代 Map 时,索引顺序正在改变......?

    <form method="POST" action="/deleteValue">
        <tr th:each="weight : ${user.weights}">
            <span th:text="${weightStat.index}">index</span>
            - <span th:text="${#dates.format(new java.util.Date(weight.key))}"></span>
            - <span th:text="${weight.value}">value</span>Kg
            <input type="hidden" name="key" th:value="${weight.key}"/>
            <button type="submit">X</button>
            <br>
        </tr>
    </form>

OUTPUT(错误顺序),H2 DB(真实顺序)

标签: spring-bootthymeleaflinkedhashmap

解决方案


我发现了问题,Hibernate 自动将 Linked 或 Tree Map 更改为经典 hashmap ... 解决方案:更改 getter

public Map<Long, Double> getWeights() { return new TreeMap<>(this.weights); }


推荐阅读