반응형

 

Template Engines 

 

템플릿 엔진 정의 

REST 웹 서비스뿐만 아니라 Spring MVC를 사용하여 동적 HTML 콘텐츠를 제공할 수 있다. Spring MVC는 Thymeleaf, Freemarker, JSP를 포함한 다양한 템플릿 기술을 지원한다. 여러 템플릿 엔진 중에서도 인기 많은 템플릿 엔진은 아래와 같다. 

 

이 4가지 중에서 내가 스프링부트를 공부하며 사용할 템플릿 엔진은 Thymeleaf 이다. 

 

Thymeleaf 

타임리프는 View Template이라고 부르는데 뷰 템플릿은 Controller가 전달하는 데이터를 이용하여 동적으로 화면을 구성할 수 있도록 해준다. 기존에 사용하던 JSP는 많은 기능 및 전체적인 화면을 디자인하는데 있어서 부족했다. 

타임리프는 HTML 기반으로 th: 속성을 이용해서 동적인 view 를 제공한다. HTML을 사용하기 때문에 진입장벽이 낮고 쉽게 배울 수 있다. 

 

Dependency 추가

우측 상단의 ADD 버튼을 클릭하여 Thymeleaf 를 선택해주면 된다. 

 

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

build.gradle 파일에서 해당 Dependency 를 확인할 수 있다.

 

 

Thymeleaf 문법

타임리프를 사용하기 위해서는 먼저 Dependency 를 추가해야하는데, 나는 start.spring.io 에서 Spring 프로젝트 생성 시 해당 Dependency 를 추가하여 사용하였다. 

<table>
  <thead>
    <tr>
      <th th:text="#{msgs.headers.name}">Name</th>
      <th th:text="#{msgs.headers.price}">Price</th>
    </tr>
  </thead>
  <tbody>
    <tr th:each="prod: ${allProducts}">
      <td th:text="${prod.name}">Oranges</td>
      <td th:text="${#numbers.formatDecimal(prod.price, 1, 2)}">0.99</td>
    </tr>
  </tbody>
</table>

문법은 HTML 태그와 유사하다. th: 를 사용하여 태그를 만들 수 있다. 

 

 

참고
https://www.thymeleaf.org/

반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 라이프코리아트위터 공유하기
  • shared
  • 카카오스토리 공유하기