【问题标题】:Thymeleaf switch case catches all casesThymeleaf 开关盒捕获所有案例
【发布时间】: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 中的字符串进行比较。

标签: spring thymeleaf


【解决方案1】:

默认大小写“*”应该放在最后。请看这里thymeleaf tutorial 然后它只会捕获非特定情况。

【讨论】:

  • 谢谢!但这仍然适用于所有其他三种情况。
  • image.status 的类型是字符串还是枚举?请问可以附上这门课吗?
  • 它是一个枚举。我附上了它的定义。
  • 可能需要转换 th:switch="${#strings.toString(image.status)}" 或类似的东西。见这里stackoverflow.com/questions/29657648/…
  • 我希望我能回答。首先在此页面中启用了百里香叶吗?我的意思是:像 th:each 这样的结构实际上列出了所有图像吗?我的观点是:从某个来源获取 th:switch 的工作示例。然后你一步一步地改变它,接近你自己的代码。在你检查回归的每一步,所以你会发现代码中的罪魁祸首。
【解决方案2】:

用于将枚举与 switch case 匹配: th:case="${T(yourpackage.GalleryImage_DetectionStatus).neverReviewed}"

【讨论】:

  • 我做到了。 ${image.status} 的评估给出了例如 neverReviewed ,它应该只触发第一种情况,但会触发所有情况。
  • 要与枚举匹配,使用案例条件如 th:case="${T(.GalleryImage_DetectionStatus).neverReviewed}"
  • 好的,谢谢。这仍然不适用于 th:switch 但我只是更改为 th:if now which works....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-10
相关资源
最近更新 更多