Thymeleaf

Yoon·2023년 2월 2일
1

Thymeleaf란?

타임리프는 흔히 View Template(뷰 템플릿)이라고 부른다.
뷰 템플릿은 컨트롤러가 전달하는 데이터를 이용하여 동적으로 화면을 구성할 수 있게 해준다.

html태그를 기반으로하여 th:속성을 이용하여 동적인 View를 제공한다.


Thymeleaf 디펜던시 추가

타임리프를 사용하기 위해서 디펜던시를 추가해야 한다.

Maven(pom.xml)

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Gradle(bulid.gradle)

dependencies {
...
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	}

Thymeleaf 기본 사용 법

타임리프 사용방법은 4가지가 있다.

  • 변수식사용 : ${}
  • 메세지방식 : #{}
  • 객체변수식 : *{}
  • 링크방식 : @{}
표현설명예제
@{ }URL 링크 표현식th:href="@{/css/bottstrap.min.css}" th:href="@{/css/bottstrap.min.css}"
${ }변수th:text=${user.name}
th:each반복출력<tr th:each="item: ${items}"> <td th:text="${item.price}">100</td></tr>
*{ }선택변수<tr th:object="${items}">
<td th:text="*{price}">100</td> </tr>
#{ }메시지.properties같은 외부 자원에서 코드에 해당하는 문자열 getth:text="#{member.register}"
|...|리터럴 대체th:text="\|Hi ${user.name}!\|" (= th:text="'Hi '+${user.name}+'!'"
profile
나의 공부 일기

0개의 댓글