- Thymeleaf란?

타임리프는 server-side의 Java 템플릿으로서,  TML, XML, JavaScript, CSS, 일반 텍스트 문서를 생성하는 View Template Engine이다.

타임리프의 목표는 우아하고, 고 유지보수성(유지보수가 용이한)의 템플릿 생성을 제공하는 것이다.


- Spring Boot2에서 Thymeleaf 적용 설정하기

1-1. Spring Initializr에서 Thymeleaf 선택하기

Spring Initializr(https://start.spring.io/)에서 Spring Boot 프로젝트를 생성할 때, Thymeleaf 항목을 추가한다.


1-2. pom.xml에서 spring-boot-starter-thymeleaf 추가하기(Maven 기준)

https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf 에서 본인이 원하는 버전에 맞는 타임리프 버전의 dependency를 추가해 준다.

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

2. application.properties에서 thymeleaf 관련 설정

application.properties에서 다음의 항목을 추가해 준다.

spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.prefix=/WEB-INF/views/
spring.thymeleaf.suffix=.html

spring.thymeleaf.enabled → thymeleaf의 사용을 enable 시킨다.

spring.thymeleaf.encoding → thymeleaf의 인코딩을 UTF-8로 기본 설정한다.

spring.thymeleaf.prefix → thymeleaf의 기본 경로인 templates를 원하는 임의의 경로로 설정한다. (여기에서는 webapps/WEB-INF/views 로 설정하였다.)

spring.thymeleaf.suffix → thymeleaf가 읽어들이는 파일의 확장자를 설정한다. 예를 들어 Controller의 method에서 return "index"; 일 경우, 자동적으로 index.html을 읽어로게 된다.


- Thymeleaf의 기본 폴더 경로

Thymeleaf에서는 기본적으로 resources 폴더의 templates폴더에 html 파일들이 위치한다.

이 외의 css, js 등의 정적 파일들은 static 폴더에 위치하게 된다.


- Trouble Shooting

1. ERROR - org.thymeleaf.exceptions.TemplateInputException: Error resolving template “index”, template might not exist or might not be accessible by any of the configured Template Resolvers

→ templates 폴더에 "index.html"파일이 없어서 발생하는 문제이다. 이 외에도 해당 경로에 대상 view가 없으면 이와 같은 에러가 발생한다.

2. 기존에 JSP를 사용하고 있다면, jstl과 jasper 관련 설정을 pom.xml에서 주석 처리 해주어야 한다.


참고

http://cyberx.tistory.com/132

http://araikuma.tistory.com/30

https://blog.hanumoka.net/2018/08/01/spring-20180801-spring-Thymeleaf/

https://www.tutorialspoint.com/spring_boot/spring_boot_thymeleaf.htm

https://www.mkyong.com/spring-boot/spring-boot-hello-world-example-thymeleaf/

http://rura6502.tistory.com/2

http://wonwoo.ml/index.php/post/1209

https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#what-is-thymeleaf

https://www.thymeleaf.org/doc/articles/standarddialect5minutes.html

https://memorynotfound.com/spring-boot-thymeleaf-configuration-example/

https://cizz3007.github.io/%ED%83%80%EC%9E%84%EB%A6%AC%ED%94%84/syntax/thymeleaf/2018/04/09/thymeleaf/


'Java Programming' 카테고리의 다른 글

Maven으로 Spring Boot 2 세팅하기  (0) 2018.12.03
Maven에서 Java Spring 세팅하기  (0) 2018.11.30
Java 101 - StringBuffer  (0) 2018.09.03
Java 101 - 생성자(Constructor)  (0) 2018.09.03

WRITTEN BY
장태희

,