Home > Back-end >  Thymeleaf switch case catches all cases
Thymeleaf switch case catches all cases

Time:11-18

I try to show different status depending on a field gallerypicture.status with thymeleaf

<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>

with galleryPicture.status defined as an enum as follows:

public enum GalleryImage_DetectionStatus {
    neverReviewed,
    partiallyReviewed,
    fullyReviewed;
}

But thymeleaf applies all 3 cases at once. What is wrong with my code?

CodePudding user response:

The default case '*' should be in the end. Please see here thymeleaf tutorial Then it will catch only non-specific cases.

CodePudding user response:

for matching enum with switch case: th:case="${T(yourpackage.GalleryImage_DetectionStatus).neverReviewed}"

  • Related