SPRING
2018.09.23 / 16:10

Thymeleaf¿¡¼­ thymeleaf-layout-dialect ¼Ò°³

hangawee
Ãßõ ¼ö 174

Thymeleaf¿¡¼­ ÆäÀÌÁö ·¹À̾ƿôÀ» µµ¿ÍÁִ¡¦

Spring boot·Î °³¹ßÀ» Çϸé È­¸é´Ü ÀÛ¾÷À» ÇÒ ¶§ ÅÛÇø´ ¿£ÁøÀ» »ç¿ëÇÏ°Ô µË´Ï´Ù.
FreemakerJSPThymeleaf¿Í °°Àº ÅؽºÆ® ÅÛÇø´ ¿£ÁøÀ» »ç¿ëÇϰųª Tiles³ª Sitemesh¿Í °°Àº ·¹À̾ƿô ÅÛÇø´ ¿£ÁøÀ» ÇÔ²² »ç¿ëÇÏ¿© È­¸é´Ü °³¹ßÀ» ÇÏ°Ô µË´Ï´Ù.
ÀÌÁß Thymeleaf¿¡¼­´Â °øÅëÀûÀ¸·Î »ç¿ëÇÏ´Â ·¹À̾ƿôÀ» ÀÛ¼ºÇϱâ À§Çؼ­´Â thymeleaf-layout-dialect¸¦ »ç¿ëÇϸé ÁÁ½À´Ï´Ù.

Thymeleaf Layout Dialect Github ÆäÀÌÁöÀÇ ¼Ò°³±ÛÀ» °£·«ÇÏ°Ô ¹ø¿ªÇÏÀÚ¸é ´ÙÀ½°ú °°½À´Ï´Ù. (¿À·ù ¹ø¿ªÀÌ ÀÖÀ» ¼ö ÀÖ½À´Ï´Ù.)

ŸÀÓ¸®ÇÁ¿¡¼­ ÄÚµå Àç»ç¿ëÀ» °³¼±Çϱâ À§ÇØ Àç»ç¿ëÀÌ °¡´ÉÇÑ ·¹À̾ƿô°ú ÅÛÇø´À» ÀÛ¼ºÇÒ ¼ö ÀÖµµ·Ï µµ¿ÍÁÖ´Â ¶óÀ̺귯¸®ÀÔ´Ï´Ù.

ÀÌ ¶óÀ̺귯¸®´Â Spring Boot 1.5.8.RELEASEÀÇ spring-boot-starter-thymeleaf ½ºÅ¸ÅÍ ÀÇÁ¸¼º¿¡ Æ÷ÇԵǾî ÀÖ½À´Ï´Ù.
¸¸¾à Spring Boot starter¸¦ »ç¿ëÇÏÁö ¾Ê°Å³ª, ´Ù¸¥ ȯ°æ¿¡¼­ »ç¿ëÇÏ½Ç °æ¿ì °ø½Ä ¹®¼­ÀÇ ¼³Ä¡ ºÎºÐÀ» Âü°íÇϽñ⠹ٶø´Ï´Ù.


thymeleaf-layout-dialectÀÇ °£´ÜÇÑ »ç¿ë ¿¹Á¦

°ø½Ä Github¿¡ ÀÖ´Â ¿¹Á¦¸¦ ±âÁØÀ¸·Î ¼³¸íµå¸®·Á ÇÕ´Ï´Ù.


1. °øÅëÀ¸·Î »ç¿ëÇÒ Layout html ÀÛ¼ºÇϱâ

¸ÕÀú ÄÁÅÙÃ÷ ÆäÀÌÁö¿¡¼­ °øÅëÀûÀ¸·Î »ç¿ëÇÒ htmlÀ» ÀÛ¼ºÇÕ´Ï´Ù.
ÀÌ °øÅëÀûÀ¸·Î »ç¿ëµÉ layout ÆÄÀϸíÀ» comm_layout.htmlÀ̶ó ÇÏ°Ú½À´Ï´Ù.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<title>Layout page</title>
<script src="common-script.js"></script>
</head>
<body>
<header>
<h1>My website</h1>
</header>
<section layout:fragment="content">
<p>Page content goes here</p>
</section>
<footer>
<p>My footer</p>
<p layout:fragment="custom-footer">Custom footer here</p>
</footer>
</body>
</html>


2. ½ÇÁ¦ ÄÁÅÙÃ÷ ÆäÀÌÁö

ÄÁÅÙÃ÷ ÆäÀÌÁö´Â ´ÙÀ½°ú °°½À´Ï´Ù.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="comm_layout">
<head>
<title>Content page</title>
<script src="content-script.js"></script>
</head>
<body>
<section layout:fragment="content">
<p>This is a paragraph from the content page</p>
</section>
<footer>
<p layout:fragment="custom-footer">This is some footer content from the content page</p>
</footer>
</body>
</html>

ÄÁÅÙÃ÷ ÆäÀÌÁö¿¡¼­ htmlÀÇ ÅÂ±× ¾È¿¡¼­ layout:decorator ¼Ó¼ºÀÌ ÀÖ½À´Ï´Ù.
ÇØ´ç ¼Ó¼ºÀº °øÅë ·¹À̾ƿôÀ» ÁöÁ¤ÇÏ´Â ¼Ó¼ºÀ¸·Î À§ 1¹ø¿¡¼­ »ç¿ëÇÑ ·¹À̾ƿô ÆÄÀϸíÀ» ÀÛ¼ºÇØÁÝ´Ï´Ù.
È®ÀåÀÚ´Â Á¦°ÅÇÑ ÆÄÀϸíÀ» Àû¾îÁÖ¸ç, ½ÇÁ¦ Spring Boot¿¡¼­ »ç¿ëÇÏ´Â °æ·Î¿¡ ¸ÂÃç¼­ ÀÛ¼ºÇÏ°Ô µË´Ï´Ù.

À§ °øÅë ·¹À̾ƿô°ú ½ÇÁ¦ ÄÁÅÙÃ÷¸¦ Àû¿ëÇÑ ÆäÀÌÁö´Â ´ÙÀ½°ú °°Àº °á°ú¹°·Î ¸¸µé¾îÁö°Ô µË´Ï´Ù.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html>
<head>
<title>Content page</title>
<script src="common-script.js"></script>
<script src="content-script.js"></script>
</head>
<body>
<header>
<h1>My website</h1>
</header>
<section>
<p>This is a paragraph from the content page</p>
</section>
<footer>
<p>My footer</p>
<p>This is some footer content from the content page</p>
</footer>
</body>
</html>

À§ÀÇ °á°ú¹°À» º¸¸é µ¿ÀÛ ¿ø¸®³ª ¹æ½Ä¿¡ ´ëÇÑ ÀÌÇØ°¡ ´õ Àß µÉ°ÍÀÔ´Ï´Ù.
°øÅë ·¹À̾ƿô¿¡ ½ÇÁ¦ ÄÁÅÙÃ÷ ÆäÀÌÁö¸¦ µ¤¾îµÐ Çü½ÄÀ¸·Î °á°ú°¡ ³ª¿À°Ô µË´Ï´Ù.
ÇÏÁö¸¸ °øÅë ·¹À̾ƿôÀÇ layout:fragment= ºÎºÐÀº ½ÇÁ¦ ÄÁÅÙÃ÷ ÆäÀÌÁöÀÇ ¼±¾ðµÈ ³»¿ëÀ¸·Î ġȯÇÏ°Ô µË´Ï´Ù.

thymeleaf-layout-dialectÀÇ layout namespace¸¦ »ç¿ëÇÏ¿© ±âº»ÀûÀÎ ¹æ¹ýÀ» ¾Ë¾ÆºÃ½À´Ï´Ù.

Thymeleaf¿¡¼­ ½ÇÁ¦·Î dialect¸¦ »ç¿ëÇÏ´Â ¿¹Á¦´Â Thymeleaf¿¡¼­ ÅÛÇø´ Çü½ÄÀ¸·Î »ç¿ëÇϱâ Æ÷½ºÆÃÀ» Âü°íÇØ ÁÖ¼¼¿ä.