spring-boot - 错误的 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(真实顺序)
解决方案
我发现了问题,Hibernate 自动将 Linked 或 Tree Map 更改为经典 hashmap ... 解决方案:更改 getter
public Map<Long, Double> getWeights() { return new TreeMap<>(this.weights); }
推荐阅读
- swift - SwiftUI:从另一个初始化程序调用它时,如何让我的闭包返回“内容”而不是“某些视图”?
- excel - VBA粘贴剪贴板内容(唯一值)
- html - 如何在不加倍边框或使用边框折叠的情况下圆桌边框?
- macos - 检测主机是否可以通过跳转主机访问
- java - Spring QueryDsl 分页过滤器按 ACL 权限
- windows - 在 Windows 10 中启用剪贴板历史记录
- laravel - 根据 Laravel 中的下拉选择发送电子邮件
- html - 如何排除 Pug 中的路径?
- c++ - 在 Visual Studio 中模拟 GCC 的 __builtin_unreachable?
- excel - 考虑在宏执行期间按下的键