[SpringBoot] 타임리프(Thymeleaf)

원서연·2023년 11월 25일
0

의존성 추가

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'         // Thymeleaf Template
	implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'               // Thymeleaf Layout
}

문법

[[...]]

html 자체에 text로 자바 객체/변수 값을 사용
ex) <div>[[answer.title]]</div>"

th:text

요소의 text에 자바 객체/변수 값을 사용
ex) <div th:text="${answer.title}></div>"

${...}

  • 자바 문법 사용
    • 자바 객체/변수 사용가능.
    • 단, Controller에서 Model 등으로 데이터를 뷰에 전달해야함

타임리프 유틸리티 (#)

  • # 으로 시작한다.
  • ${#lists.size(question.answerList)}
  • 유틸리티 목록
    • #lists.size(..) : 컬렉션의 요소 개수
    • {#temporals.format(날짜 객체, 포맷) : 날짜와 시간을 포맷팅

자바 객체와 문자열 혼용 (|...|)

<span th:text="|welcome ${@rq.member.username}|"></span>
profile
웹 백엔드 프로그래밍 Today I Learned

0개의 댓글