티스토리 뷰

Thymeleaf는 웹 환경과 비웹 환경 모두에서 작동 할 수있는 Java XML / XHTML / HTML5 템플릿 엔진 - Wikipedia

 

1. fragment (include)   

  메뉴 또는 상단, 푸터 등 공통으로 사용되는 파일을 삽입시 사용

  • 선언 : th:fragement="fragment명"
  • 사용(호출) : th:replace="경로/파일명 :: fragment명"
<!-- 메뉴(선언) templates/include/menu.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <body>
    	<div th:fragement="menu">
        메뉴1 | 메뉴2 | 메뉴3
        </div>
    </body>
</html>    


<!-- 본문(호출) templates/index.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <body>
    	<div th:fragement="menu" th:replace="include/menu :: menu">
        </div>
    </body>
</html>   
파라미터 사용 예시
<!-- 본문(호출) templates/include/header.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:fragment="head(title)">
  <title th:text="${title}">Hellow, Spring Boot!</title>
</head>  
    <body>
    	<div>
         
        </div>
    </body>
</html>    


<!-- 본문(호출) templates/index.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="/include/header :: head('게시판')">
</head>  
    <body>
    	<div>
        </div>
    </body>
</html>   

 

 

 

2.  link (링크)

  • 선언 : th:href="@{링크경로/파일명}"
<link href="sticky-footer-navbar.css" th:href="@{/sticky-footer-navbar.css}" rel="stylesheet">

 

3. 리스트 Size 출력

  • ${#lists.size()} 
  • ${#lists.isEmpty()}
[JAVA]

ArrayList<String> colors = new ArrayList<>(Arrays.asList("Black", "White", "Green", "Red"));


[Thymeleaf]

<span th:text="${#lists.size(colors)}">Size 출력</span>

<span th:if="${#lists.isEmpty(colors)}">empty 여부</span>

 

4. object (객체 속성 접근)

  • 부모 태그의 th:object로 지정된 객체의 속성에 접근 
<table>
  <tr>
    <th>성명</th>
    <th>닉네임</th>
    <th>아이디</th>
  </tr>
  <tr th:object="${session.memInfo>">
    <td><span th:text="*{userName}"></span></td>
    <td><span th:text="*{userNickName}"></span></td>
    <td><span th:text="*{userId}"></span></td>
  </tr>	
</table>

일일히 ${session.memInfo.userName} 와 같이 기술해서 무관하다

 

반응형
댓글
반응형
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday