Cute Blinking Unicorn

백엔드/스프링부트

스프링MVC와 Thymeleaf || Thymeleaf를 사용하는 법

민밥통 2024. 1. 11. 10:40

스프링부트 ex3 프로젝트 새로 생성. 

  • spring Boot DevTools
  • Lombok
  • Spring Web
  • Thumeleaf

application.properties (캐싱 금지)

#캐싱금지
spring.thymeleaf.cache=false

Thymeleaf를 이용하는 프로젝트는 변경 후에 만들어진 결과를 보관(캐싱)하지 않도록 설정해두는 것이 편리함.

controller

package org.zerock.ex3.controller;

import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/sample")
@Log4j2
public class SampleController {
    @GetMapping("/ex1")
    public void ex1() {
        log.info("ex1................");
    }
}

동작을 확인하기 위해서 @Log4j2를 적용함 LomBok의 기능으로 스프링 부트가 라이브러리 중에 Log4j2를 기본으로 사용하기 있기 때문에 별도의 설정 없이 적용이 가능 

emplates.samle로 패키지 추가후

ex1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1 th:text="${'Hello World'}"></h1>
    [['test']]
</body>
</html>

localhost:8080/sample/ex1

이렇게 뜬다. [['test']]는 태그에 속하지 않으려면 이렇게 구문을 사용한다. 

JSP에서 퍼센트 기호를 써서 여러가지 하는 것보다 훨씬 단순하다는 점.