๐ŸŒฟSpring/Spring Framework

[Spring Framework - Thymeleaf] Thymeleaf ๋ž€?

Boom's 2024. 1. 18. 12:50
๋ฐ˜์‘ํ˜•

 

Thymeleaf ๋ž€?

 

  • Thymeleaf๋Š” ์„œ๋ฒ„ ์ธก ์ž๋ฐ” ์›น ํ…œํ”Œ๋ฆฟ ์—”์ง„
  • HTML, XML, JavaScript, CSS๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฐ” ํ…œํ”Œ๋ฆฟ ์—”์ง„
  • ์ฃผ๋กœ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์„ฑ ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

 

Thymeleaf์˜ ๋ชฉํ‘œ


 

์œ ์ง€ ๊ด€๋ฆฌ๊ฐ€ ์‰ฌ์šด ํ…œํ”Œ๋ฆฟ ์ƒ์„ฑ ๋ฐฉ๋ฒ•์„ ์ œ๊ณต

์‹ค์ œ๋กœ ํ…œํ”Œ๋ฆฟ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š”
(HTML์˜ ๊ตฌ์กฐ๋ฅผ ๊นจ์ง€ ์•Š๊ณ , ๊ธฐ์กด HTML ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ๋ง๋ถ™์ด๋Š” ์ฝ”๋“œ) ๋ฐฉ์‹์„ ์‚ฌ์šฉ

 

 

  • Natural Templates ๊ฐœ๋…์„ ๊ธฐ๋ฐ˜
  • ์ด๋ฅผ ํ†ตํ•œ ๋””์ž์ธ ํŒ€๊ณผ ๊ฐœ๋ฐœ ํŒ€๊ฐ„์˜ ๊ฐˆ๋“ฑ๊ณผ ๊ฒฉ์ฐจ ํ•ด์†Œ๋ฅผ ๊ธฐ๋Œ€

Thymeleaf ์ฃผ์š” ํŠน์ง•

  1. ์ž์—ฐ์Šค๋Ÿฌ์šด ๋ฌธ๋ฒ•: Thymeleaf๋Š” HTML ๋ฌธ์„œ์— ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ†ตํ•ฉ๋˜๋Š” ๋ฌธ๋ฒ•์„ ์ œ๊ณต.
    • ์ด๋Š” HTML์„ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•˜๋ฉด์„œ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.
  2. ํ…œํ”Œ๋ฆฟ ์žฌ์‚ฌ์šฉ: ํ…œํ”Œ๋ฆฟ์˜ ์žฌ์‚ฌ์šฉ์ด ๊ฐ„๋‹จ, ํ”„๋ ˆ๊ทธ๋จผํŠธ(fragment)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ผ๋ถ€ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ณ  ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  3. ๋‹ค์–‘ํ•œ ์ปจํ…์ŠคํŠธ ์ง€์›: Thymeleaf๋Š” ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋‹ค์–‘ํ•œ ์ปจํ…์ŠคํŠธ์—์„œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, ์›น ์„œ๋ฒ„, ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ, ์Šคํ”„๋ง ๋ถ€ํŠธ์™€ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  4. ์„œ๋ฒ„ ์ธก ๋ Œ๋”๋ง: Thymeleaf๋Š” ์„œ๋ฒ„ ์ธก์—์„œ ๋ Œ๋”๋ง๋˜๋ฏ€๋กœ, ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ ๋„ ์™„์ „ํ•œ ํŽ˜์ด์ง€๋ฅผ ์ƒ์„ฑ
  5. ํ†ตํ•ฉ ์šฉ์ด์„ฑ: Thymeleaf๋Š” ๋‹ค์–‘ํ•œ ์ž๋ฐ” ์›น ํ”„๋ ˆ์ž„์›Œํฌ์™€์˜ ํ†ตํ•ฉ์ด ์ž˜ ๋˜์–ด ์žˆ๋‹ค.
    • ํŠนํžˆ ์Šคํ”„๋ง๊ณผ์˜ ํ†ตํ•ฉ์ด ๊ฐ•๋ ฅํ•˜๋ฉฐ, ์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ๋Š” ๊ธฐ๋ณธ ํ…œํ”Œ๋ฆฟ ์—”์ง„์œผ๋กœ ์‚ฌ์šฉ

Thymeleaf ์„ค์ •


  • ์˜์กด์„ฑ (Denpendency) ์„ค์ • - build.grad
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
}

 

Thymeleaf ๊ธฐ๋ณธ ํ‹€


  • ์˜์กด์„ฑ (Denpendency) ์„ค์ • - build.grad
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Thymeleaf Example</title>
</head>
<body>
    <h1 th:text="${message}">Hello, World!</h1>
</body>
</html>

 

  • XML ๋„ค์ž„์ŠคํŽ˜์Šค์˜ ์„ ์–ธ โžก๏ธ <html xmlns:th="http://www.thymeleaf.org">

Dialect - Standard Dialect

  • Thymeleaf์˜ ํ•ต์‹ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Standard Dialect๋ผ๋Š” dialect๋ฅผ ์ œ๊ณต
ํ”„๋กœ์„ธ์Šค๊ฐ€ ์—†๊ณ , ์ „์ฒด์ ์œผ๋กœ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ์•„ํ‹ฐํŒฉ๋“œ๋“ค๋กœ ๊ตฌ์„ฑ ๋  ์ˆ˜ ์žˆ์ง€๋งŒ, ํ”„๋กœ์„ธ์„œ๋Š” ํ™•์‹คํžˆ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ ์‚ฌ๋ก€

 

Standard Dialect์˜ ๋Œ€๋ถ€๋ถ„์œผ ํ”„๋กœ์„ธ์„œ๋Š” 'Attribute Processor'์ด๋‹ค.

์ด๋ฅผ ํ†ตํ•ด ๋ธŒ๋ผ์šฐ์ €๋Š” ๋‹จ์ˆœํžˆ ์ถ”๊ฐ€ ์†์„ฑ์„ ๋ฌด์‹œํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜๋ฆฌ๋˜๊ธฐ ์ „์—๋„ HTML ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ํ‘œ์‹œ

 

# JSP

<input type="text" name="userName" value="${user.name}">

 

# Thymeleaf Standard Dialect

<input type="text" name="userName" value="Gorany" th:value="${user.name}">

 

 

Template Engine์˜ ๊ตฌ์„ฑ

Spring์„ ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค๋ฉด XML๋˜๋Š” Java Config๋กœ ViewResolver๋ฅผ ๋‹ค๋ค„๋ณด์•˜๋˜ ๊ฒฝํ—˜์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค.

๊ทผ๋ฐ ์Šคํ”„๋ง ๋ถ€ํŠธ๋กœ ๋„˜์–ด์™€์„œ Thymeleaf๋ฅผ ์“ธ ๋•Œ, ViewResolver๋ฅผ ์„ค์ •ํ•œ ์ ์ด ์—†๋Š”๋ฐ resources/template ๋ฐ‘์— HTML ํŒŒ์ผ์„ ๋„ฃ์–ด๋‘๋ฉด Controller์—์„œ ํฌ์›Œ๋”ฉ์„ ํ•ด์ค€๋‹ค.

 

Template Engine์„ ๋ฉค๋ฒ„๋กœ ๊ฐ–๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ดˆ๊ธฐํ™”(์ƒ์„ฑ)๋  ๋•Œ, ServletContextTemplateResolver ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋ฉฐ, ์ด templateResolver์— prefix(์ ‘๋‘์‚ฌ)์™€ suffix(์ ‘๋ฏธ์‚ฌ)๋ฅผ ์„ค์ •ํ•˜๊ณ , Template Engine ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ์—”์ง„์— ํ…œํ”Œ๋ฆฟ ๋ฆฌ์กธ๋ฒ„๋ฅผ ๊ฝ‚์•„์ฃผ๋Š” ์‹์ด๋‹ค.

 

์ฝ”๋“œ๋ฅผ ๋“ค์—ฌ๋‹ค๋ณด๋ฉด ๋Œ€์ถฉ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (WEB-INF == resources)

public class GTVGApplication {
  
    
    ...
    private final TemplateEngine templateEngine;
    ...
    
    
    public GTVGApplication(final ServletContext servletContext) {

        super();

        ServletContextTemplateResolver templateResolver = 
                new ServletContextTemplateResolver(servletContext);
        
        // HTML is the default mode, but we set it anyway for better understanding of code
        templateResolver.setTemplateMode(TemplateMode.HTML);
        // This will convert "home" to "/WEB-INF/templates/home.html"
        templateResolver.setPrefix("/WEB-INF/templates/");
        templateResolver.setSuffix(".html");
        // Template cache TTL=1h. If not set, entries would be cached until expelled
        templateResolver.setCacheTTLMs(Long.valueOf(3600000L));
        
        // Cache is set to true by default. Set to false if you want templates to
        // be automatically updated when modified.
        templateResolver.setCacheable(true);
        
        this.templateEngine = new TemplateEngine();
        this.templateEngine.setTemplateResolver(templateResolver);
        
        ...

    }

}

 

๋ฐ˜์‘ํ˜•