【发布时间】:2021-12-28 07:17:07
【问题描述】:
我尝试根据带有 thymeleaf 的字段 gallerypicture.status 显示不同的状态
<tr th:each="image: ${galleryPictures}" class=".col-sm-7">
<td th:switch="${image.status}">
<p th:case="'neverReviewed'">
<div class="img-container" style="max-width: 50px" data-toggle="tooltip" data-placement="top" th:title="#{root.client.mitarbeiter.detectionTable.neverReviewed}">
<img th:src="@{/img/Check_blue_icon.png}" style="width: 50%; height: 50%; object-fit: contain;">
</div>
</p>
<p th:case="'partiallyReviewed'">
<div class="img-container" style="max-width: 50px" data-toggle="tooltip" data-placement="top" th:title="#{root.client.mitarbeiter.detectionTable.partiallyReviewed}">
<img th:src="@{/img/Check_orange_icon.png}" style="width: 50%; height: 50%; object-fit: contain;">
</div>
</p>
<p th:case="'fullyReviewed'">
<div class="img-container" style="max-width: 50px" data-toggle="tooltip" data-placement="top" th:title="#{root.client.mitarbeiter.detectionTable.fullyReviewed}">
<img th:src="@{/img/Check_green_icon.png}" style="width: 50%; height: 50%; object-fit: contain;">
</div>
</p>
<p th:case="'*'">
<div class="img-container" style="max-width: 50px" data-toggle="tooltip" data-placement="top" th:title="#{root.client.mitarbeiter.detectionTable.neverReviewed}">
<img th:src="@{/img/Check_blue_icon.png}" style="width: 50%; height: 50%; object-fit: contain;">
</div>
</p>
</td>
galleryPicture.status 定义为枚举,如下所示:
public enum GalleryImage_DetectionStatus {
neverReviewed,
partiallyReviewed,
fullyReviewed;
}
但 thymeleaf 一次应用所有 3 个案例。我的代码有什么问题?
【问题讨论】:
-
注意默认大小写是
th:case="*",而不是th:case="'*'"。 -
关于@rushitvaishnani 建议的方法:这应该有效。如果你想让你的开关工作,你可以edit你的问题并向我们展示你对这种方法的尝试。 (但我知道您现在可以使用
if语句。)另一种方法是为您的所有枚举赋值 -neverReviewed("neverReviewed")(并将必要的字段、构造函数和 getter 添加到枚举中)然后您 将能够将它们与 Thymeleaf 中的字符串进行比较。