programing

티멜리프에서의 if-else는 어떻게 합니까?

procenter 2022. 8. 10. 22:03
반응형

티멜리프에서의 if-else는 어떻게 합니까?

심플하게 하려면 어떻게 하면 좋을까요?if-else티멜리프에서요?

나는 티멜리프에서도 같은 효과를 얻고 싶다.

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

JSTL에서.

내가 지금까지 생각한 것은:

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

평가하고 싶지 않다potentially_complex_expression두 번이나요 그래서 지역 변수를 도입했어요condition그래도 둘 다 쓰는 건 싫어th:if="${condition}그리고.th:unless="${condition}".

중요한 것은 두 개의 HTML 태그를 사용한다는 것입니다.h2그리고.span.

그것을 달성할 수 있는 더 나은 방법을 제안해 주시겠습니까?

티멜리프는 다음과 같다.<c:choose>그리고.<c:when>: 그th:switch그리고.th:caseTymeleaf 2.0에서 도입된 속성입니다.

예상대로 동작합니다.*디폴트 케이스의 경우:

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

구문(또는 Tymeleaf 튜토리얼)에 대한 간단한 설명은 이 항목을 참조하십시오.

면책사항:StackOverflow 규칙에 따라 저는 Tymeleaf의 저자입니다.

이 코드를 사용하여 고객이 로그인하고 있는지 익명으로 되어 있는지 확인했습니다.사용했어요.th:if그리고.th:unless조건식꽤 간단한 방법이죠

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

다니엘 페르난데스 씨 외에 보안과 관련된 제 사례를 말씀드리겠습니다.

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

다음은 'authentication' 유틸리티 개체와 'authorization' 유틸리티 개체가 혼합된 복잡한 표현식입니다. 이 유틸리티 개체는 Tymeleaf 템플릿 코드에 대해 'true/false' 결과를 생성합니다.

'authentication' 및 'authorization' 유틸리티 오브젝트는 thymeleaf extra spring security 3 라이브러리에서 가져옵니다.authentication' 개체를 사용할 수 없는 경우 또는 authorization.expression('is)Authenticated()'는 'false'로 평가되며 식은 ${false}를 반환하고 그렇지 않으면 ${true}를 반환합니다.

사용할 수 있습니다.

If-then-else:  (if) ? (then) : (else)

예제:

'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))

같은 질문을 하는 새로운 사람들에게 유용할 수 있다.

다른 솔루션 - 로컬 변수를 사용할 수 있습니다.

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

로컬 변수에 대한 자세한 내용:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#local-variables

간단한 경우(html 태그가 동일한 경우):

<h2 th:text="${potentially_complex_expression} ? 'Hello' : 'Something else'">/h2>

또 다른 솔루션은 다음과 같습니다.not반대되는 부정을 얻으려면:

<h2 th:if="${potentially_complex_expression}">Hello!</h2>
<span class="xxx" th:if="${not potentially_complex_expression}">Something else</span>

설명서에서 설명한 바와 같이, 이것은 다음 명령어를 사용하는 것과 같습니다.th:unless다른 답변에서 설명한 바와 같이:

또한.th:if는 역속성을 가지고 있습니다.th:unless이전 예에서는 OGNL 식에 포함되지 않은를 사용하는 대신 사용할 수 있었습니다.

사용.notIMHO도 사용할 수 있지만 IMHO를 사용하는 것이 더 읽기 쉽습니다.th:unless병세를 부정하는 대신not.

사용하다th:switchif-else

<span th:switch="${isThisTrue}">
  <i th:case="true" class="fas fa-check green-text"></i>
  <i th:case="false" class="fas fa-times red-text"></i>
</span>

th:switchswitch

<span th:switch="${fruit}">
  <i th:case="Apple" class="fas fa-check red-text"></i>
  <i th:case="Orange" class="fas fa-times orange-text"></i>
  <i th:case="*" class="fas fa-times yellow-text"></i>
</span>
<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>
<div style="width:100%">
<span th:each="i : ${#numbers.sequence(1, 3)}">
<span th:if="${i == curpage}">
<a href="/listEmployee/${i}" class="btn btn-success custom-width" th:text="${i}"></a
</span>
<span th:unless="${i == curpage}">
<a href="/listEmployee/${i}" class="btn btn-danger custom-width" th:text="${i}"></a> 
</span>
</span>
</div>

여기에 이미지 설명 입력

이것은, 유저의 성별에 따라 사진을 표시하고 싶은 경우에 유효합니다.

<img th:src="${generou}=='Femenino' ? @{/images/user_mujer.jpg}: @{/images/user.jpg}" alt="AdminLTE Logo" class="brand-image img-circle elevation-3">

언급URL : https://stackoverflow.com/questions/13494078/how-to-do-if-else-in-thymeleaf

반응형