Jak zrobić if-else w Thymeleaf?
Jaki jest najlepszy sposób na proste if-else w Thymeleaf?
Chcę osiągnąć w tym samym efekcie co
<c:choose>
<c:when test="${potentially_complex_expression}">
<h2>Hello!</h2>
</c:when>
<c:otherwise>
<span class="xxx">Something else</span>
</c:otherwise>
</c:choose>
W JSTL.
Co do tej pory wymyśliłem:
<div th:with="condition=${potentially_complex_expression}" th:remove="tag">
<h2 th:if="${condition}">Hello!</h2>
<span th:unless="${condition}" class="xxx">Something else</span>
</div>
Nie chcę oceniać potentially_complex_expression
dwa razy. Dlatego wprowadziłem zmienną lokalną condition
.
Nadal nie lubię używać obu th:if="${condition}
i th:unless="${condition}"
.
Ważne jest to, że używam 2 różnych tagów html: powiedzmy h2
i span
.
Czy możesz zaproponować lepszy sposób na osiągnięcie to?
8 answers
Thymeleaf ma odpowiednik <c:choose>
i <c:when>
: atrybuty th:switch
i th:case
wprowadzone w Thymeleaf 2.0.
Działają zgodnie z oczekiwaniami, używając *
dla domyślnego przypadku:
<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="#{roles.manager}">User is a manager</p>
<p th:case="*">User is some other thing</p>
</div>
Zobacz http://www.thymeleaf.org/whatsnew20.html#swit dla szybkiego wyjaśnienia składni (lub tutoriale thymeleaf).
Disclaimer, zgodnie z zasadami Stoskoverflow: jestem autorem thymeleaf.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-04 11:31:03
Próbowałem tego kodu, aby dowiedzieć się, czy klient jest zalogowany lub anonimowy. Użyłem th:if
i th:unless
wyrażeń warunkowych. Całkiem prosty sposób.
<!-- IF CUSTOMER IS ANONYMOUS -->
<div th:if="${customer.anonymous}">
<div>Welcome, Guest</div>
</div>
<!-- ELSE -->
<div th:unless="${customer.anonymous}">
<div th:text=" 'Hi,' + ${customer.name}">Hi, User</div>
</div>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-17 08:42:54
Chciałbym podzielić się moim przykładem związanym z bezpieczeństwem oprócz Daniela Fernándeza.
<div th:switch="${#authentication}? ${#authorization.expression('isAuthenticated()')} : ${false}">
<span th:case="${false}">User is not logged in</span>
<span th:case="${true}">Logged in user</span>
<span th:case="*">Should never happen, but who knows...</span>
</div>
Tutaj jest złożone wyrażenie z mieszanymi obiektami narzędziowymi "authentication" i "authentication", które generuje wynik "true/false" dla kodu szablonu thymeleaf.
Obiekty użytkowe' authentication 'i' authentication ' pochodzą z biblioteki Thymeleaf extras springsecurity3 . Gdy obiekt "authentication" nie jest dostępny lub Autoryzacja.expression ('isAuthenticated ()') ocenia do' false ' wyrażenie zwraca ${false}, w przeciwnym razie ${true}.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-17 07:33:20
Możesz użyć
If-then-else: (if) ? (then) : (else)
Przykład:
'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))
To może być przydatne dla nowych ludzi zadających to samo pytanie.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-09 15:50:20
W prostszym przypadku (gdy znaczniki html są takie same):
<h2 th:text="${potentially_complex_expression} ? 'Hello' : 'Something else'">/h2>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-01 16:23:30
Inne rozwiązanie - możesz użyć zmiennej lokalnej:
<div th:with="expr_result = ${potentially_complex_expression}">
<div th:if="${expr_result}">
<h2>Hello!</h2>
</div>
<div th:unless="${expr_result}">
<span class="xxx">Something else</span>
</div>
</div>
Więcej o lokalnym variables:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#local-variables
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-17 08:36:10
Innym rozwiązaniem jest użycie not
, Aby uzyskać odwrotną negację:
<h2 th:if="${potentially_complex_expression}">Hello!</h2>
<span class="xxx" th:if="${not potentially_complex_expression}">Something else</span>
Jak wyjaśniono w dokumentacji , jest to to samo, co użycie th:unless
. Jak wyjaśniły inne odpowiedzi:
Również
th:if
posiada atrybut odwrotności,th:unless
, który możemy mieć użyty w poprzednim przykładzie zamiast użycia not wewnątrz OGNL wyrażenie
Używanie not
również działa, ale IMHO bardziej czytelne jest użycie th:unless
zamiast negowania warunek z not
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-02 00:36:44
<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="#{roles.manager}">User is a manager</p>
<p th:case="*">User is some other thing</p>
</div>
<div th:with="condition=${potentially_complex_expression}" th:remove="tag">
<h2 th:if="${condition}">Hello!</h2>
<span th:unless="${condition}" class="xxx">Something else</span>
</div>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-08 07:44:02