๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ ์ด์•ผ๊ธฐ/Springboot

[thymeleaf] layout decorate ์กฐ๊ฑด ๋ถ€์—ฌ - @{}, @{||}

by 0_0๐Ÿ–ค 2023. 11. 26.
๋ฐ˜์‘ํ˜•

 

 

 

ํ”„๋กœ์ ํŠธ ์œ ์ง€๋ณด์ˆ˜๋ฅผ ์œ„ํ•œ ๋ฆฌํŒฉํ† ๋ง ์ž‘์—… ๋„์ค‘

ํ•˜๋‚˜์˜ ํŽ˜์ด์ง€์—์„œ ์‚ฌ์šฉ์ž ๊ถŒํ•œ์— ๋”ฐ๋ผ

thymeleaf์˜ layout decorate์˜ ๊ฒฝ๋กœ๊ฐ€ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๋„๋ก ์ฒ˜๋ฆฌํ•ด์•ผ ํ–ˆ๋‹ค.

 

์ฒ˜์Œ์—” layout:decorate ์ž‘์„ฑํ•˜๋Š” html ํƒœ๊ทธ ๋ถ€๋ถ„์„

th:block์œผ๋กœ ๊ฐ์‹ธ th:if๋ฅผ ์ด์šฉํ•ด ์ฒ˜๋ฆฌํ•˜๋ ค ํ–ˆ๋‹ค.

 

 

<!-- ์‚ฌ์šฉ์ž ๊ถŒํ•œ์ด ํŒ๋งค์ž ๊ถŒํ•œ์ผ ๊ฒฝ์šฐ '/seller/layout/default' ๊ฒฝ๋กœ๋กœ ์„ค์ • -->
<th:block th:if="${session.STYPECODE == 'mem_type_02'
                    || session.STYPECODE == 'mem_type_03'
                    || session.STYPECODE == 'mem_type_04'}">
                    
    <html xmlns:th="http://www.thymeleaf.org"
          xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
          layout:decorate="~{'/seller/layout/default'}">
    
</th:block>

<!-- ์‚ฌ์šฉ์ž ๊ถŒํ•œ์ด ๊ด€๋ฆฌ์ž ๊ถŒํ•œ์ผ ๊ฒฝ์šฐ '/admin/layout/default' ๊ฒฝ๋กœ๋กœ ์„ค์ • -->
<th:block th:if="${(session.STYPECODE == 'mem_type_01')}">

    <html xmlns:th="http://www.thymeleaf.org"
          xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
          layout:decorate="~{'/admin/layout/default'}">

</th:block>

 

 

 

๊ทธ๋Ÿฌ๋‚˜ ์ด๋ ‡๊ฒŒ ์ฒ˜๋ฆฌํ•  ๊ฒฝ์šฐ 500 ์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค.

 

java.lang.IllegalArgumentException: layout:decorate/data-layout-decorate must appear in the root element of your template

 

 

๋ฌธ์ œ์˜ ์˜ค๋ฅ˜๋Š” layout:decorate๋‚˜ data-layout-decorate๊ฐ€ ํ…œํ”Œ๋ฆฟ์˜ ์ตœ์ƒ์œ„ ์—˜๋ฆฌ๋จผํŠธ์— ๋‚˜ํƒ€๋‚˜์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

๊ฒฐ๊ตญ ํ•ด๋‹น ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” th:block ์ชฝ์„ ์ œ๋Œ€๋กœ ์ฝ์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ถ”์ •

 

์ผ๋‹จ html ํƒœ๊ทธ์— ์žˆ๋Š” xmlns:th, xmlns:layout, layout:decorate ๊ฐ€ ๋จผ์ € ์ž‘์„ฑ ๋˜์–ด ์žˆ์–ด์•ผ th:block์ด๋‚˜ th:if๋„ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ธ๋ฐ th:if๊ฐ€ ์ฝํžˆ๊ธฐ ์ „์— ์ด๋Ÿฌํ•œ ์„ ์–ธ๋ถ€๊ฐ€ ์ž‘์„ฑ๋˜์ง€ ์•Š์•„์„œ ์•ˆ๋˜๋Š” ๊ฒƒ ์ผ๊นŒ?

 

 

 

 

 

๋‘ ๋ฒˆ์งธ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด ์ž‘์—…ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

<html xmlns:th="http://www.thymeleaf.org"
          xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
          layout:decorate="@{|${(session.STYPECODE == 'mem_type_02'
                              || session.STYPECODE == 'mem_type_03'
                              || session.STYPECODE == 'mem_type_04')? '/seller/layout/default'
                               :(session.STYPECODE == 'mem_type_01')? '/admin/layout/default':''}|}">

 

 

์ด๋Ÿฐ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ–ˆ๋”๋‹ˆ ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋œ๋‹ค.

 

 

์ข€ ๋” ์•Œ์•„๋ณด๋‹ค๋ณด๋‹ˆ

๋‚ด๊ฐ€ ์‚ฌ์šฉํ•œ @{| |} ํ‘œํ˜„์‹์€ ์„œ๋ฒ„ ๋‚ด๋ถ€์—์„œ ์ฒดํฌํ•˜์ง€ ์•Š๊ณ 

ํด๋ผ์ด์–ธํŠธ๋กœ ์ง์ ‘ ์ „๋‹ฌํ•˜๋Š” ํ‘œํ˜„์‹, 

@{ } ๋กœ ์ž‘์„ฑํ•  ๊ฒฝ์šฐ ์„œ๋ฒ„์—์„œ ์กฐ๊ฑด์„ ํ‰๊ฐ€ ํ›„ ํด๋ผ์ด์–ธํŠธ๋กœ ์ „๋‹ฌํ•˜๋Š” ์‹์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ์•˜๋‹ค.

 

์„œ๋ฒ„์˜ ๋กœ์ง์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ๋ ˆ์ด์•„์›ƒ์„ ์„ ํƒํ•ด์•ผ ํ•  ๋•Œ๋Š” @{ }๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์œ ์šฉํ•˜๊ณ 

์ด๋ฏธ ์กฐ๊ฑด์— ๋”ฐ๋ฅธ ๋ฌธ์ž์—ด์ด ์„œ๋ฒ„์—์„œ ์ƒ์„ฑ๋˜์–ด ์žˆ๊ณ 

ํด๋ผ์ด์–ธํŠธ๋กœ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌํ•˜๋ ค๋ฉด @{| |} ํ‘œํ˜„์‹์ด ์œ ์šฉํ•  ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.

 

<!-- ์˜ˆ์ œ 1 -->
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="@{${session.STYPECODE == 'mem_type_02'
                        or session.STYPECODE == 'mem_type_03'
                        or session.STYPECODE == 'mem_type_04'} ? '/seller/layout/default'
                       : ${session.STYPECODE == 'mem_type_01'} ? '/admin/layout/default' : ''}">

<!-- ์˜ˆ์ œ 2 -->
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="@{|${session.STYPECODE == 'mem_type_02'
                         or session.STYPECODE == 'mem_type_03'
                         or session.STYPECODE == 'mem_type_04'} ?'/seller/layout/default'
                        : ${session.STYPECODE == 'mem_type_01'} ? '/admin/layout/default' : ''}|">

 

 

 

์ƒํ™ฉ์— ๋”ฐ๋ผ ์˜ˆ์ œ1 ํ˜น์€ ์˜ˆ์ œ2 ํ˜•ํƒœ๋กœ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ

๋‚˜๋Š” ์ด๋ฏธ ์ปจํŠธ๋กค๋Ÿฌ์— ๊ฒฝ๋กœ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ์œผ๋‹ˆ ์˜ˆ์ œ 2๋ฒˆ์œผ๋กœ ์„ ํƒํ•ด ์ž‘์—…ํ–ˆ๋‹ค.

 

 

 

 

+ ์ˆ˜์ •

 

์˜ˆ์ œ 2๋ฒˆ์œผ๋กœ ํ–ˆ์„ ๋•Œ ๋กœ์ปฌ ์„œ๋ฒ„์—์„œ๋Š” ๋ฌธ์ œ๊ฐ€ ์—†์œผ๋‚˜

jenkins๋ฅผ ์ด์šฉํ•ด ๋ฐฐํฌ ํ–ˆ์„ ๊ฒฝ์šฐ ์ •์ƒ์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์•˜๋‹ค....

 

 

<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{|${(session.STYPECODE == 'mem_type_02'
                                || session.STYPECODE == 'mem_type_03'
                                || session.STYPECODE == 'mem_type_04')? 'seller/layout/default'
                                :(session.STYPECODE == 'mem_type_01')? 'admin/layout/default':''}|}">

 

 

~{ | | } ๋กœ ๋ฐ”๊ฟ”์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

 

 

 

 

๋ฐ˜์‘ํ˜•