2023. 3. 16. 11:15ใSpring
Spring ๊ตฌ์กฐ ์ก๊ธฐ ๋ฉ๋ด ๋ณธ๋ฌธ ๋ถ๋ฆฌ
jsp์ ํ๋ฉด ๋ถํ > template
spring > fragment (thymeleaf์์ ์ง์ํ๋ ๊ธฐ๋ฅ)
build.gradle์ dependencies ๋งจ ์ฒซ์ค์ ์๋ ์ฝ๋ ๋ฃ์ด์ผ ํ๋ฉด๋ถํ ๊ธฐ๋ฅ(fragment)์ ์ ๊ณตํด์ค๋ค.
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
์ ์ฅ ํ gradle refresh ํด์ฃผ๊ธฐ!
๊ธฐ๋ฅ์ ์คํํ ์ปจํธ๋กค๋ฌ ๋ง๋ค์ด์ค
templates ๋ฐ์ fragment์ content ํด๋ ์์ฑ
fragmentํด๋ ์์ header.html ์์ฑ - ๋ฉ๋ด ๋ถ๋ถ
user_layout.html ์์ฑ - ์ ์ ๊ฐ ๋ณด๋ ๋ชจ๋ html ์ด๋ฆฌ๋ layout ํ์ผ
user_layout.html์ ์๋จ์ html xmlns:th~ ํ์๋ฆฌํ ์ค์ ํ <>๋ซํ๊ธฐ ์ ์ ์ํฐ ์น๊ณ ๋ค์ฌ์ฐ๊ธฐ ํด์ ์๋์ฝ๋ ์ถ๊ฐ
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
th:block ๋ง๋ค์ด์ ํค๋ ๋ถ๋ถ์ ๋ถ๋ฌ์ด
<!-- th:replace=fragment/header : templates/fragment/header.html ํ์ผ ์ฐพ์๊ฐ๋ค๋ ์๋ฏธ -->
<!-- ::์ ์๋ฏธ๋ ๊ฒฝ๋ก์ ์ด๋ฆ์ ๋ถ์ฌํ๋ ๊ฒ -->
<th:block th:replace="fragment/header::headerFragment"></th:block>
layout์์ header.html์ ๋ถ๋ฌ์ค๋ฉด ์์ title, meta ๋ฑ๋ฑ์ด ๊ฒน์น๊ธฐ ๋๋ฌธ์
header.html ๋ด์ฉ์ html ํ๊ทธ ๋นผ๊ณ ๋ค ์ง์์ฃผ๊ธฐ.
layout.html์ ๋์ฒดํ ํ๋ฉด ๊ฒฝ๋ก ์ง์ ํ ๊ฒ์ header.html์๋ ์ ์ด์ค.
๋ณธ๋ฌธ์ ํด๋นํ๋ board_list์ html์ ์๋ ๋ ์ฝ๋ ์ถ๊ฐ
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{fragment/user_layout}"
board_list.html๋ layout.html๊ณผ ๋ด์ฉ ๊ฒน์น์ง ์๊ฒ html ํ๊ทธ ๋นผ๊ณ ๋ด์ฉ ์ง์์ค.
th:block ์์ layout:fragment="content"์ถ๊ฐํ์ฌ ๋ด์ฉ๋ถ๋ถ์์ ์ค์ (?
layout.html์์๋ content ๋ถ๋ฌ์ค๋ ์ฝ๋ ์ ์ด์ค.
runํ๋ฉด ํ ํ์ด์ง์์ ๋ค ์ด๋ฆฌ๋ ๊ฒ ํ์ธ ๊ฐ๋ฅ!
board_detail.html ๋ง๋ค๊ณ board_list์์ a ํ๊ทธ ์ฌ์ฉํ์ฌ ์์ธ๋ณด๊ธฐ ์์ฑ
์์ธ๋ณด๊ธฐ ํด๋ฆญํ๋ฉด list ์ ์ธ ํค๋๋ ๋ ์ด์์ ํ์ผ ๊ฒ์๊ธ ์์ธ ํ์ด์ง ๋ค ๊ฐ์ด ์ด๋ฆฌ๊ฒ ์์ .
ํ๋ฉด ์ฌ์ด๋ ๊ด๋ฆฌ์ ๋ฉ๋ด ๋์ฐ๊ธฐ
fragment์ admin_layout.html ์์ฑ ํ ์๋ ์ฝ๋ ์ ๊ธฐ
fragment์ side.html ์์ฑ ์๋ ์ฝ๋ ์์ฑ
content ํด๋ ๋ฐ์ admin.html ์์ฑ ์๋ ์ฝ๋ ์์ฑ
๊ด๋ฆฌ์ ํ์ด์ง๋ก ์ด๋ํ๋ฉด
header์ side ๋ฉ๋ด ๊ฐ์ด ๋์ด.
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BootStrap (๋ฐ์ํ ์น ์ฌ์ดํธ) (0) | 2023.03.17 |
---|---|
spring - ์ธ๋ถ css/javascript ๋ก๋ฉ (0) | 2023.03.17 |
Spring ๊ฒ์ํ ํ๋ก์ ํธ (0) | 2023.03.15 |
Spring ๋ฐ์ดํฐ ๋๊ธฐ๊ณ ๋ฐ๊ธฐ(์์ธ) (0) | 2023.03.15 |
Spring - DB ํ์ ๊ด๋ฆฌ ์ค์ต (0) | 2023.03.15 |