Programming/Web-Spring

[SpringBoot] Spring 프로젝트에서 Thymeleaf 기본 설정하는 방법

아이바 2023. 6. 14. 10:06

먼저, thymeleaf를 설정하는 방법을 소개 한 후,

thymeleaf 설정 후 css도 설정하는 방법을 알려드리겠습니다.

 

Thymeleaf 설정하는 방법

STEP 1. build/gradle 파일 설정

 아래와 같이 thymeleaf 관련 dependency를 추가합니다.

..
dependencies {
	..
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation('nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect')
}Copy

 

STEP 2. application.properties 설정

아래의 4줄을 추가합니다.

spring.thymeleaf.cache=false
spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.htmlCopy

 

STEP 3. html 파일 상단에 thymeleaf 관련 태그 추가

아래의 첫줄인 <html xmlns.. 로 시작하는 것을 추가합니다.

<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
....Copy

 

CSS 설정하는 방법

STEP 1. src/main/resources/ 폴더에 static 폴더를 추가합니다.

(src/main/resources/static)

예를들어 style.css를 추가했다고 가정해보겠습니다.

 

STEP 2. html파일에 CSS 추가

아래의 <link> 태그를 참고하여, 

style.css 파일과 연동하게 만듭니다.

<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">

        <link rel="stylesheet" href="style.css" type="text/css">
        <title></title>
        ...
728x90