HTML

[Thymeleaf] 프로젝트에 사용한 타임리프 문법 정리

sian han 2022. 8. 19. 00:23

※ How to use multiple condition in if tag using thymeleaf ? 

타임리프에서 이중 if 사용

타임리프에서 if and

 

'and' 사용

<button type="button" class="btn btn-outline-dark" 
  th:if="(${session.role}=='user') and (${item.sellerId}==${session.id})">
  
  수정하기</button>

session 에 저장된 role 값이 "user" 이고,

item의 sellerId 값이 session 의 id 값과 동일하다면

'수정하기' 버튼을 보여줘라

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


※ How to formate LocalDateTime in Thymeleaf ? 

타임리프에서 LocalDateTime 타입 format

 

 

temporals.format 사용

<input type="text" class="form-control" aria-label="Username"
                                       aria-describedby="basic-addon1" th:value="${#temporals.format(item.getRegTime(),'yyyy-MM-dd')}">

${#temporals.format(LocalDateTime,'yyyy-MM-dd')}

 

 

 

 

 

 

 

 

 

 

 


※ How to formate number in Thymeleaf ? 

1000단위 쉼표 찍기

 

 

numbers.formatInteger 사용

<label>총결제금액 : </label>
<span th:text="${#numbers.formatInteger(order.getTotalPrice(), 3, 'COMMA') + ' 원'}">
</span>

 

 

 

 


※ How to put variable to img src path with thymeleaf?

타임리프에서 이미지 경로설정 (절대경로 + 컨트롤러에서 model 로 내려준 변수)

 

@{} + ${} 형태 사용

<img th:src="@{/uploadImg/} + ${item.getOrginFileName()}" alt="${item.itemName}">

 

가운데 + 빨간줄 오류뜨는데 무시하면 됨

 

 

 

 

 

 

 

 

 

 


※ How to Compare enum with value from controller ?

 

enum 변수 값 비교하기

<th:block th:if="${item.sellStatus==T(com.proj.KnitMarket.Constant.SellStatus).SELL}">
    <button th:formaction="@{'/knitmarket/cart/' + ${item.id} }" type="submit" class="btn btn-outline-success" >장바구니</button>
    <button type="button" class="btn btn-outline-danger">구매하기</button>
</th:block>
<div th:unless="${item.sellStatus==T(com.proj.KnitMarket.Constant.SellStatus).SELL}">
    <h5>품절된 상품입니다</h5>
</div>

unless : else

 

 

 

 

 

 

 


※ a Tag onclick alert confirm

a 태그 클릭 시 confirm alert 뜬 후 href 주소로 이동

 

<a th:href="@{'/knitmarket/cartRemove/' + ${cartItem.id} }" id='delBtn' th:onclick ="return confirm('장바구니에서 삭제하시겠습니까 ?');">

 

 

 

 

 

 


※ button onclick get

 

th:formaction

<button class="btn btn-dark" th:formaction="@{'/order/orderList/' + ${order.user.id} }">주문목록</button>

 

근데 이게 이제 안먹힐때가 있음 이유는 모르겠음, 그럴때는

 

th:onclick

<button class="btn btn-dark" th:onclick="|location.href='@{/order/orderList/} + ${order.user.id}'|">주문목록</button>

 

 

 

++ 계속추가예정

'HTML' 카테고리의 다른 글

[HTML] CSS 스타일속성 2  (0) 2022.04.20
[HTML] CSS 스타일속성 1  (0) 2022.04.20
[HTML] CSS, 선택자  (0) 2022.04.19
[HTML] 태그  (0) 2022.04.19