๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

SRPING

Thymeleaf

1. What is Thymeleaf

Thymeleaf๋Š” server-side JAVA ๊ธฐ๋ฐ˜์˜ template engine ์ด๋‹ค.

Spring boot ๊ฐ€ ๊ถŒ์žฅํ•˜๋Š” ํ…œํ”Œ๋ฆฟ ์—”์ง„ ์ค‘ ํ•˜๋‚˜์ด๋‹ค.(Groovy, Thymeleaf, FreeMarker..) ๊ทธ๋™์•ˆ ์‚ฌ์šฉํ•ด์™”๋˜ jsp๋ณด๋‹ค ๋” ๊ฐ„๋‹จํ•œ ์„ค์ •๊ณผ ํ‘œ์ค€ ๋ฌธ๋ฒ•์œผ๋กœ HTML์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. Thymeleaf๋Š” ์›น ํ‘œ์ค€, ํŠนํžˆ HTML5 ์— ๋งž์ถฐ ์„ค๊ณ„๋˜์—ˆ๊ธฐ์— ์™„์ „ํžˆ ๊ฒ€์ฆ๋œ ํ…œํ”Œ๋ฆฟ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

JSP์˜ ๊ฒฝ์šฐ, tag libraries๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, HTML ํ‘œ์ค€ ํƒœ๊ทธ๊ฐ€ ์•„๋‹ˆ๊ธฐ์— ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ํ•ด์„ํ•  ์ˆ˜ ์—†๋Š” ์ฝ”๋“œ๊ฐ€ ๋œ๋‹ค. 

<form:inputText name="userName" value="${user.name}" />

Thymeleaf๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž‘์„ฑํ•œ ๊ฒฝ์šฐ, ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๊ฐ€ ๋œ๋‹ค. ๋ฌธ๋ฒ• ์—ญ์‹œ ๊น”๋”ํ•˜๊ฒŒ ๋ณด์ธ๋‹ค.

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

 

์ด๋กœ ์ธํ•ด ํผ๋ธ”๋ฆฌ์…”, ๋””์ž์ธ๊ณผ ๊ฐœ๋ฐœํŒ€์˜ ๊ฒฉ์ฐจ๋ฅผ ํ•ด์†Œํ•˜๋ฉฐ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์„ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ์–ดํ•„ํ•œ๋‹ค.(๊ณต์‹ doc์—์„œ..)

์ด๋ฒˆ์— ์‚ฌ์šฉํ•ด๋ณด๋‹ˆ ๊ฐœ๋ฐœํ•  ๋•Œ๋„ Thymeleaf ๊ฐ€ ํ›จ์”ฌ ํŽธํ•˜๋‹ค. jsp์˜ ๊ฒฝ์šฐ view ํ…œํ”Œ๋ฆฟ ๋ ˆ๋”๋ง์„ Servlet Container๊ฐ€ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋Š”๋ฐ ๋”œ๋ ˆ์ด๊ฐ€ ์žˆ๋‹ค. ๋ฐ˜๋ฉด Thymeleaf์˜ ๊ฒฝ์šฐ๋Š” Servlet Container ๊ฐœ์ž…์—†์ด ์ตœ์ข… View๋ฅผ ์™„์„ฑํ•˜๋ฏ€๋กœ, ๋ฐ”๋กœ ํ™•์ธ์ด ์ˆ˜์›”ํ•˜๋‹ค. ํ…Œ์ŠคํŠธ ์‹œ์—๋„ Servlet Container๋ฅผ ๋„์šธ ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ mockMVC์œผ๋กœ๋„ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๋งŒ์•ฝ frond-end ๊ฐœ๋ฐœ์ž๊ฐ€ Thymeleaf์—†์ด view๋ฅผ ๋žœ๋”๋งํ•ด์„œ ๋งŒ๋“ค๊ณ  ์žˆ๋‹ค๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด, ์ด๋ฅผ ๊ณ ๋ คํ•˜์—ฌ attribute ๋„ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•œ๋‹ค.. 

 

 

2. Standard Expression Syntax

[Simple expressions]

Variable Expressions: ${...}

Selection Variable Expressions: *{...}

Message Expressions: #{...}

Link URL Expressions: @{...}

Fragment Expressions: ~{...}

 

[Text operations]

String concatenation: +

Literal substitutions: |The name is ${name}|

 

 

3. Thymeleaf Doc

 

Tutorial: Using Thymeleaf

1 Introducing Thymeleaf 1.1 What is Thymeleaf? Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text. The main goal of Thymeleaf is to provide a

www.thymeleaf.org