错误的 Thymeleaf 迭代
Wrong Thymeleaf iteration
我不知道为什么,但是当我用 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); }
我不知道为什么,但是当我用 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); }