๋ฐ์ํ
- ํ์๋ฆฌํ ์ ์ฉ
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
- code
- HTML์ ํ์๋ฆฌํ๋ฅผ ์ฐ๊ฒฐํ๋ค. HTML์ ํ์ฉํด์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ natural template์ด๋ผ๊ณ ํ๋ค.
- th ๋ผ๋ ์ฝ์ด๋ก ํ์๋ฆฌํ๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ค.
- code
- ์ฌ์ฉ๋ฒ1 : item์ด๋ ์ด๋ฆ์ผ๋ก ๋ก์ง์์ List๋ฅผ ๋ฐํํ์ ๊ฒฝ์ฐ(json์ผ๋ก ๋ฐํ๋ ๊ฒฝ์ฐ)
- ํ๋ฉด
- code๋๋ณด๊ธฐ
<!DOCTYPE HTML> <html lang="ko" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8"> <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet"> </head> <body> <div class="container" style="max-width: 600px"> <div class="py-5 text-center"> <h2>์ํ ๋ชฉ๋ก</h2> </div> <div class="row"> <div class="col"> <button class="btn btn-primary float-end" th:onclick="|location.href='@{/basic/items/add}'|" type="button">์ํ ๋ฑ๋ก</button> </div> </div> <hr class="my-4"> <div> <table class="table"> <thead> <tr> <th>ID</th> <th>์ํ๋ช </th> <th>๊ฐ๊ฒฉ</th> <th>์๋</th> </tr> </thead> <tbody> <tr th:each="item : ${items}"> <td><a href="item.html" th:href="@{/basic/items/{itemId}(itemId=${item.id})}" th:text="${item.id}">ํ์ id</a></td> <td><a href="item.html" th:href="@{/basic/items/{itemName}(itemName=${item.itemName})}"th:text="${item.itemName}">์ํ๋ช </a></td> <td th:text="${item.price}">10000</td> <td th:text="${item.quantity}">10</td> </tr> </tbody> </table> </div> </div> <!-- /container --> </body> </html>
- <tr th:each="item : ${items}">
→ foreach๋ฌธ๊ณผ ๊ฐ๋ค. items์ ์๋ ๊ฑธ item์ด๋ ์ด๋ฆ์ผ๋ก ํ๋์ฉ ๊บผ๋ด์จ๋ค. - th:text="${item.id}"
→ ํ ์คํธ๋ฅผ html์ ๋ฃ์์ด๋ th์์ ์ง์ ํ ํ ์คํธ๋ก ์นํ๋๋ค. - <a href="item.html" th:href="@{/basic/items/{itemId}(itemId=${item.id})}" th:text="${item.id}">ํ์ id</a>
→ ๐ href๊ฐ ์ฐ์์ง๋ง, th:href ํ์๋ฆฌํ๋ก ์นํ๋๋ค.
→ ๋งํฌ ํํ์ : href="@{ }" ์์ผ๋ก ๊ฒฝ๋ก๋ฅผ ์ ์ ์ ์๋ค.
→ {itemId}(itemId=${item.id}) ์ฒ๋ผ ์์ ๊ฐ์ ์ง์ ํด ์ ์ ์ ์๋ค. - ๐ ๋ฆฌํฐ๋ด ๋์ฒด |....| ๋ฌธ๋ฒ
- ์ ์๋ <span th:text="'welcome' + ${user.name} + '!'">์ผ๋ก ์ ์ด์ผ ํ์๋ค.
- ์ง๊ธ์ <span th:text="|welcome ${user.name}!"> ์ผ๋ก ์ ์ ์ ์๋ค.
- ํ๋ฉด
- ์ฌ์ฉ๋ฒ2 : item์ด๋ ์ด๋ฆ์ผ๋ก (ํด๋์ค๋ก ๋ง๋ ) ๊ฐ์ฒด๋ฅผ ๋ฐํ ๋ฐ์์ ๊ฒฝ์ฐ. (json์ผ๋ก ๋ค์ด์จ๋ค.)
- ํ๋ฉด
- code
๋๋ณด๊ธฐ<!DOCTYPE HTML> <html lang="ko" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8"> <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet"> <style> .container { max-width: 560px; } </style> </head> <body> <div class="container"> <div class="py-5 text-center"> <h2>์ํ ์์ธ</h2> </div> <div> <label for="itemId">์ํ ID</label> <input type="text" id="itemId" name="itemId" class="form-control" value="1" th:value=${item.id} readonly> </div> <div> <label for="itemName">์ํ๋ช </label> <input type="text" id="itemName" name="itemName" class="form-control" value="์ํA" th:value=${item.itemName} readonly> </div> <div> <label for="price">๊ฐ๊ฒฉ</label> <input type="text" id="price" name="price" class="form-control" value="10000" th:value=${item.price} readonly> </div> <div> <label for="quantity">์๋</label> <input type="text" id="quantity" name="quantity" class="form-control" value="10" th:value=${item.quantity} readonly> </div> <hr class="my-4"> <div class="row"> <div class="col"> <button class="w-100 btn btn-primary btn-lg" onclick="location.href='editForm.html'" th:onclick="|location.href='@{/basic/items/{itemId}/edit(itemId=${item.id})}'|" type="button">์ํ ์์ </button> </div> <div class="col"> <button class="w-100 btn btn-secondary btn-lg" onclick="location.href='items.html'" th:onclick="|location.href='@{/basic/items}'|" type="button">๋ชฉ๋ก์ผ๋ก</button> </div> </div> </div> <!-- /container --> </body> </html>
- th:value
→ ๋ง ๊ทธ๋๋ก value. ๊ฐ์ผ๋ก ๋ค์ด๊ฐ๋ค.
- ํ๋ฉด
- ์ฌ์ฉ๋ฒ3 : ์์ฒญ ์ ๋ฌ
- ํ๋ฉด
- code
- ์คํ๋ง์ ๋น์ ๋จผ์ ์กฐํํ๊ธฐ ๋๋ฌธ์ th:action์ ์ ์ธ ์ด๋ฆ๋๋ก ๋น(์ปจํธ๋กค๋ฌ)๋ฅผ ์ฐพ์๊ฐ๋ค.
- ์ด ๋ add๋ผ๋ ์ด๋ฆ์ผ๋ก method๋ฐฉ์๋ง get/post๋ฅผ ๋ฌ๋ฆฌํด์ ์ด๋ฆ์ด ๊ฐ๊ธฐ์ action์ฃผ์ ์๋ต๊ฐ๋ฅ.
- ํ๋ฉด
- ์ฌ์ฉ๋ฒ4
- ๋ก์ง์์ RedirectAttributes๋ก ๋ณด๋์ ๊ฒฝ์ฐ์
- th:if="${param.status}"
ํํ๋ก ๋ฐ๋ก ๋ฐ์ ์ ์๋ค.
param์ด๋ ์ด๋ฆ์ ๋ทฐํ ํ๋ฆฟ์ด ์ ๊ณตํ๋ค. - d
- d
โป ์ฐธ์กฐ : https://www.inflearn.com/roadmaps/373
๋ฐ์ํ
'โจ UI > ๐ค View Templates' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Thymeleaf] ํ์๋ฆฌํ๋? (0) | 2025.03.07 |
---|---|
[JAVA]JSP๋ก ํ์๊ด๋ฆฌ ์น ์ ํ๋ฆฌ์ผ์ด์ ๋ง๋ค๊ธฐ (0) | 2025.02.28 |
[JSP] jsp๋? ๊ธฐ๋ณธ๊ตฌ์ฑ (0) | 2024.12.31 |