-
[๊ฐ์] ์คํ๋ง MVC 2ํธ - ๋ฐฑ์๋ ์น ๊ฐ๋ฐ ํต์ฌ ๊ธฐ์ 1SPRING/INFLEARN 2022. 11. 21. 04:33
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-mvc-2
์คํ๋ง MVC 2ํธ - ๋ฐฑ์๋ ์น ๊ฐ๋ฐ ํ์ฉ ๊ธฐ์ - ์ธํ๋ฐ | ๊ฐ์
์น ์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ์ ํ์ํ ๋ชจ๋ ์น ๊ธฐ์ ์ ๊ธฐ์ด๋ถํฐ ์ดํดํ๊ณ , ์์ฑํ ์ ์์ต๋๋ค. MVC 2ํธ์์๋ MVC 1ํธ์ ํต์ฌ ์๋ฆฌ์ ๊ตฌ์กฐ ์์ ์ค๋ฌด ์น ๊ฐ๋ฐ์ ํ์ํ ๋ชจ๋ ํ์ฉ ๊ธฐ์ ๋ค์ ํ์ตํ ์ ์
www.inflearn.com
1. ํ์๋ฆฌํ - ๊ธฐ๋ณธ ๊ธฐ๋ฅ
โ ํ๋ก์ ํธ ์์ฑ
โ ํ์๋ฆฌํ ์๊ฐ
- ํน์ง
์๋ฒ ์ฌ์ด๋ HTML ๋ ๋๋ง (SSR)
๋ค์ธ๋ด ํ ํ๋ฆฟ (์์ HTML์ ๊ทธ๋๋ก ์ ์งํ๋ฉด์ ๋ทฐ ํ ํ๋ฆฟ๋ ์ฌ์ฉํ ์ ์์)
์คํ๋ง ํตํฉ ์ง์
- ๊ณต์ ๋ฌธ์
https://www.thymeleaf.org/Thymeleaf
Integrations galore Eclipse, IntelliJ IDEA, Spring, Play, even the up-and-coming Model-View-Controller API for Java EE 8. Write Thymeleaf in your favourite tools, using your favourite web-development framework. Check out our Ecosystem to see more integrati
www.thymeleaf.org
โ ํ ์คํธ - text, utext
@Controller @RequestMapping("/basic") public class BasicController { @GetMapping("text-basic") public String textBasic(Model model){ model.addAttribute("data", "Hello Spring!"); return "basic/text-basic"; } }
๋ก data๋ผ๋ ๋ณ์์ Hello Spring!์ ๋ด์ text-basic์ด๋ผ๋ ํ์ผ๋ก ๋๊ฒผ์
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>์ปจํ ์ธ ์ ๋ฐ์ดํฐ ์ถ๋ ฅํ๊ธฐ</h1> <ul> <li>th:text ์ฌ์ฉ <span th:text="${data}"></span></li> <li>์ปจํ ์ธ ์์์ ์ง์ ์ถ๋ ฅํ๊ธฐ = [[${data}]]</li> </ul> </body> </html>
์คํ์์ผ๋ณด๋ฉด html์ฝ๋์ ์์ ๊ฐ์ด ์ ๋ค์ด๊ฐ์ ๋์ค๋ ๊ฒ์ ํ์ธํ ใ ใ ์์
- Escape๋ฅผ ์กฐ์ฌํด๋ผ
๋ณ์ ๊ฐ์ html ํ๊ทธ๋ฅผ ๋ถ์ด๋ฉด ๊ทธ๋๋ก ๋ค์ด๊ฐ ex) <b> <b&rt;
ํ๊ทธ๋ฅผ ์ ๋ง ์ถ๋ ฅํ๊ณ ์ถ์ ๋๋ฅผ ์ํด ๋ ๋ค ์ถ๋ ฅ์ผ๋ก ํต์ผํ๊ธฐ ๋๋ฌธ(Escape ์ฒ๋ฆฌ)-> ์ฒ๋ฆฌํ์ง ์์ผ๋ ค๋ฉด?
@Controller @RequestMapping("/basic") public class BasicController { ... @GetMapping("text-unescaped") public String textUnescaped(Model model){ model.addAttribute("data", "Hello <b>Spring!</b>"); return "basic/text-unescaped"; } }
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>text vs utext</h1> <ul> <li>th:text = <span th:text="${data}"></span></li> <li>th:utext = <span th:utext="${data}"></span></li> </ul> <h1><span th:inline="none">[[...]] vs [(...)]</span></h1> <ul> <li><span th:inline="none">[[...]] = </span>[[${data}]]</li> <li><span th:inline="none">[(...)] = </span>[(${data})]</li> </ul> </body> </html>
๊ดํธ๋ฅผ ๋ค๋ฅด๊ฒํ๋ฉด unescaped๋ฅผ ํ ์ ์๋ค.
ํ์ง๋ง escape๊ฐ ํ์ํ ๊ฒฝ์ฐ๊ฐ ํจ์ฌ ๋ง๊ธฐ ๋๋ฌธ์(ex ๊ฒ์ํ) ๊ธฐ๋ณธ์ escaped
โ ๋ณ์ - SpringEL
Object, List, Map์ ๋ค๋ฃจ๋ ๋ฐฉ๋ฒ์ ๋ณด๋๋ก ํ๊ฒ ๋ค.
์ ๋ฆฌ๊ฐ ์ ๋์ด์๋ค. ์ ๋ ๊ฒ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
<div th:with="first=${users[0]}"> <p>์ฒ์ ์ฌ๋์ ์ด๋ฆ์ <span th:text="${first.username}"></span></p> </div>
์ด๋ ๊ฒ ํ๋ฉด ๋ณ์์ฒ๋ผ๋ ์ธ ์ ์๋ค.
โ ๊ธฐ๋ณธ ๊ฐ์ฒด๋ค
ํ์๋ฆฌํ๋ ๊ธฐ๋ณธ ๊ฐ์ฒด๋ค์ ์ ๊ณตํ๋ค
@GetMapping("/basic-objects") public String basicObjects(HttpSession session) { session.setAttribute("sessionData", "Hello Session"); return "basic/basic-objects"; } @Component("helloBean")//์คํ๋ง ๋น ์์ฑ static class HelloBean { public String hello(String data) { return "Hello " + data; } }
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>์ ๊ธฐ๋ณธ ๊ฐ์ฒด (Expression Basic Objects)</h1> <ul> <!--HttpReauestํด์ model๋ก ๋๊ฒจ๋ ๋๋๋ฐ ์์ฃผ ์ฐ๋ ๊ฑฐ๋ผ ์ด๋ฏธ ์ง์์ค--> <li>request = <span th:text="${#request}"></span></li> <li>response = <span th:text="${#response}"></span></li> <li>session = <span th:text="${#session}"></span></li> <li>servletContext = <span th:text="${#servletContext}"></span></li> <li>locale = <span th:text="${#locale}"></span></li> </ul> <h1>ํธ์ ๊ฐ์ฒด</h1> <ul> <!--ํ๋ผ๋ฏธํฐ ๊ฐ์ ธ์ค๊ธฐ--> <li>Request Parameter = <span th:text="${param.paramData}"></span></li> <!--์ธ์ ์ ๋ด์๋ ๋ฐ์ดํฐ(๊ณ์ ํ๋๊ฑฐ)--> <li>session = <span th:text="${session.sessionData}"></span></li> <!--์คํ๋ง ๋น์ ์ง์ ์ ๊ทผ--> <li>spring bean = <span th:text="${@helloBean.hello('Spring!')}"></span></li> </ul> </body> </html>
โ ์ ํธ๋ฆฌํฐ ๊ฐ์ฒด์ ๋ ์ง
๋ง๊ณ ๊ฐ๋จํด์ ๊ทธ๋ฅ ์ฐธ๊ณ
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
์๋ฐ 8 ๋ ์ง๋ง ํ ๋ฒ ๋ณด์
๊ธฐ๋ณธ์ผ๋ก java8time ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ๋ค์ด๊ฐ ์๋ค. ๊ทธ๋ฅ ์ฌ์ฉํ๋ฉด ๋จ
java8 ๋ ์ง์ฉ ์ ํธ๋ฆฌํฐ ๊ฐ์ฒด - #temporals
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>LocalDateTime</h1> <ul> <li>default = <span th:text="${localDateTime}"></span></li> <li>yyyy-MM-dd HH:mm:ss = <span th:text="${#temporals.format(localDateTime,'yyyy-MM-dd HH:mm:ss')}"></span></li> </ul> <h1>LocalDateTime - Utils</h1> <ul> <li>${#temporals.day(localDateTime)} = <span th:text="${#temporals.day(localDateTime)}"></span></li> <li>${#temporals.month(localDateTime)} = <span th:text="${#temporals.month(localDateTime)}"></span></li> <li>${#temporals.monthName(localDateTime)} = <span th:text="${#temporals.monthName(localDateTime)}"></span></li> <li>${#temporals.monthNameShort(localDateTime)} = <span th:text="${#temporals.monthNameShort(localDateTime)}"></span></li> <li>${#temporals.year(localDateTime)} = <span th:text="${#temporals.year(localDateTime)}"></span></li> <li>${#temporals.dayOfWeek(localDateTime)} = <span th:text="${#temporals.dayOfWeek(localDateTime)}"></span></li> <li>${#temporals.dayOfWeekName(localDateTime)} = <span th:text="${#temporals.dayOfWeekName(localDateTime)}"></span></li> <li>${#temporals.dayOfWeekNameShort(localDateTime)} = <span th:text="${#temporals.dayOfWeekNameShort(localDateTime)}"></span></li> <li>${#temporals.hour(localDateTime)} = <span th:text="${#temporals.hour(localDateTime)}"></span></li> <li>${#temporals.minute(localDateTime)} = <span th:text="${#temporals.minute(localDateTime)}"></span></li> <li>${#temporals.second(localDateTime)} = <span th:text="${#temporals.second(localDateTime)}"></span></li> <li>${#temporals.nanosecond(localDateTime)} = <span th:text="${#temporals.nanosecond(localDateTime)}"></span></li> </ul> </body> </html>
โ URL ๋งํฌ
@GetMapping("/link") public String link(Model model) { model.addAttribute("param1", "data1"); model.addAttribute("param2", "data2"); return "basic/link"; }
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>URL ๋งํฌ</h1> <ul> <li><a th:href="@{/hello}">basic url</a></li> <li><a th:href="@{/hello(param1=${param1}, param2=${param2})}">hello query param</a></li> <li><a th:href="@{/hello/{param1}/{param2}(param1=${param1}, param2=${param2})}">path variable</a></li> <li><a th:href="@{/hello/{param1}(param1=${param1}, param2=${param2})}">path variable + query parameter</a></li> </ul> </body> </html>
ํ์ดํผ ๋งํฌ๋ฅผ ๋๋ฅด๋ฉด ์ ๋ค์ด๊ฐ์ง๋ ๊ฒ ํ์ธ
๋งจ ์์ / ์์ฐ๋ฉด ์๋๊ฒฝ๋ก
โ ๋ฆฌํฐ๋ด
- ๋ฌธ์, ์ซ์, Boolean, null
- ๋ฌธ์ ๋ฆฌํฐ๋ด์ ์์น์ ''๋ก ๊ฐ์ธ์ผ ํ๋ค.(๋จ์ด ์ฌ์ด์ ๊ณต๋ฐฑ์ด ์์ ๊ฒฝ์ฐ ๋ด์ฃผ๊ธฐ๋ ํจ)
@GetMapping("/literal") public String literal(Model model) { model.addAttribute("data", "Spring!"); return "basic/literal"; }
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>๋ฆฌํฐ๋ด</h1> <ul> <!--์ฃผ์! ๋ค์ ์ฃผ์์ ํ๋ฉด ์์ธ๊ฐ ๋ฐ์ํจ--> <!-- <li>"hello world!" = <span th:text="hello world!"></span></li>--> <li>'hello' + ' world!' = <span th:text="'hello' + ' world!'"></span></li> <li>'hello world!' = <span th:text="'hello world!'"></span></li> <li>'hello ' + ${data} = <span th:text="'hello ' + ${data}"></span></li> <li>๋ฆฌํฐ๋ด ๋์ฒด ๋ฌธ๋ฒ |hello ${data}| = <span th:text="|hello ${data}|"></span></li> </ul> </body> </html>
โ ์ฐ์ฐ
์ผ๋ฐ ์ฐ์ฐ์ด๋ค. ๋ค๋ง <, >์ฐ๋ ๋ถ๋ถ๋ง ์ฃผ์ํ์.
@GetMapping("/operation") public String operation(Model model) { model.addAttribute("nullData", null); model.addAttribute("data", "Spring!"); return "basic/operation"; }
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <ul> <li>์ฐ์ ์ฐ์ฐ <ul> <li>10 + 2 = <span th:text="10 + 2"></span></li> <li>10 % 2 == 0 = <span th:text="10 % 2 == 0"></span></li> </ul> </li> <li>๋น๊ต ์ฐ์ฐ <ul> <li>1 > 10 = <span th:text="1 > 10"></span></li> <li>1 gt 10 = <span th:text="1 gt 10"></span></li> <li>1 >= 10 = <span th:text="1 >= 10"></span></li> <li>1 ge 10 = <span th:text="1 ge 10"></span></li> <li>1 == 10 = <span th:text="1 == 10"></span></li> <li>1 != 10 = <span th:text="1 != 10"></span></li> </ul> </li> <li>์กฐ๊ฑด์ <ul> <li>(10 % 2 == 0)? '์ง์':'ํ์' = <span th:text="(10 % 2 == 0)?'์ง์':'ํ์'"></span></li> </ul> </li> <li>Elvis ์ฐ์ฐ์ <ul> <li>${data}?: '๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.' = <span th:text="${data}?: '๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.'"></span></li> <li>${nullData}?: '๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.' = <span th:text="${nullData}?:'๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.'"></span></li> </ul> </li> <li>No-Operation <ul> <!--๋ง์น ํ์๋ฆฌํ๊ฐ ์คํํ์ง ์๋ ๊ฒ์ฒ๋ผ ๋์--> <li>${data}?: _ = <span th:text="${data}?: _">๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.</span></li> <li>${nullData}?: _ = <span th:text="${nullData}?: _">๋ฐ์ดํฐ๊ฐ์์ต๋๋ค.</span></li> </ul> </li> </ul> </body> </html>
โ ์์ฑ ๊ฐ ์ค์
@GetMapping("/attribute") public String attribute() { return "basic/attribute"; }
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>์์ฑ ์ค์ </h1> <!--th๊ฐ name์ ๋์ฒดํ๋ค--> <input type="text" name="mock" th:name="userA" /> <h1>์์ฑ ์ถ๊ฐ</h1> <!--์์ฑ ์ด์ด๋ถ์ด๊ธฐ--> - th:attrappend = <input type="text" class="text" th:attrappend="class='large'" /><br/> <!--์์ฑ ์์ ๋ถ์ด๊ธฐ--> - th:attrprepend = <input type="text" class="text" th:attrprepend="class='large'" /><br/> <!--์์ฑ ์ด๋ฆ์ผ๋ก ์์ฐ์ค๋ฝ๊ฒ ๋ถ์ด๊ธฐ--> - th:classappend = <input type="text" class="text" th:classappend="large"/><br/> <h1>checked ์ฒ๋ฆฌ</h1> - checked o <input type="checkbox" name="active" th:checked="true" /><br/> - checked x <input type="checkbox" name="active" th:checked="false" /><br/> - checked=false <input type="checkbox" name="active" checked="false" /><br/> </body> </html>
html์ checked ์์ฑ๋ง ์์ด๋ ์ฒดํฌํ์ ํด๋ฒ๋ฆผ
-> ํ์๋ฆฌํ๋ checked์ธ ๊ฒฝ์ฐ์๋ง false๋ฉด ์ฌ๋ผ์ง
โ ๋ฐ๋ณต
th:each๋ List, Map๋ฑ ๋ชจ๋ Iterable, Enumeration ๊ฐ์ฒด๋ค์ ๋ฐ๋ณต์ ์ฌ์ฉํ ์ ์๋ค.
@GetMapping("/each") public String each(Model model) { addUsers(model); return "basic/each"; } private void addUsers(Model model) { List<User> list = new ArrayList<>(); list.add(new User("userA", 10)); list.add(new User("userB", 20)); list.add(new User("userC", 30)); model.addAttribute("users", list); }
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>๊ธฐ๋ณธ ํ ์ด๋ธ</h1> <table border="1"> <tr> <th>username</th> <th>age</th> </tr> <tr th:each="user : ${users}"> <td th:text="${user.username}">username</td> <td th:text="${user.age}">0</td> </tr> </table> <h1>๋ฐ๋ณต ์ํ ์ ์ง</h1> <table border="1"> <tr> <th>count</th> <th>username</th> <th>age</th> <th>etc</th> </tr> <tr th:each="user, userStat : ${users}"> <td th:text="${userStat.count}">username</td> <td th:text="${user.username}">username</td> <td th:text="${user.age}">0</td> <td> index = <span th:text="${userStat.index}"></span> count = <span th:text="${userStat.count}"></span> size = <span th:text="${userStat.size}"></span> even? = <span th:text="${userStat.even}"></span> odd? = <span th:text="${userStat.odd}"></span> first? = <span th:text="${userStat.first}"></span> last? = <span th:text="${userStat.last}"></span> current = <span th:text="${userStat.current}"></span> </td> </tr> </table> </body> </html>
userstat - ๋ฐ๋ณต์ ์ํ
{ ๋ณ์๋ช } + Stat ๊ผด๋ก ์ฐ๊ฒ ๋ค๋ฉด ์ ์ธ ์๋ตํด๋ ๋๋ค.
โ ์กฐ๊ฑด๋ถ ํ๊ฐ
if์ switch๋ฌธ * ์ default
@GetMapping("/condition") public String condition(Model model) { addUsers(model); return "basic/condition"; }
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>if, unless</h1> <table border="1"> <tr> <th>count</th> <th>username</th> <th>age</th> </tr> <tr th:each="user, userStat : ${users}"> <td th:text="${userStat.count}">1</td> <td th:text="${user.username}">username</td> <td> <span th:text="${user.age}">0</span> <span th:text="'๋ฏธ์ฑ๋ ์'" th:if="${user.age lt 20}"></span> <span th:text="'๋ฏธ์ฑ๋ ์'" th:unless="${user.age ge 20}"></span> </td> </tr> </table> <h1>switch</h1> <table border="1"> <tr> <th>count</th> <th>username</th> <th>age</th> </tr> <tr th:each="user, userStat : ${users}"> <td th:text="${userStat.count}">1</td> <td th:text="${user.username}">username</td> <td th:switch="${user.age}"> <span th:case="10">10์ด</span> <span th:case="20">20์ด</span> <span th:case="*">๊ธฐํ</span> </td> </tr> </table> </body> </html>
โ ์ฃผ์
@GetMapping("/comments") public String comments(Model model) { model.addAttribute("data", "Spring!"); return "basic/comments"; }
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>์์</h1> <span th:text="${data}">html data</span> <h1>1. ํ์ค HTML ์ฃผ์</h1> <!-- <span th:text="${data}">html data</span> --> <h1>2. ํ์๋ฆฌํ ํ์ ์ฃผ์</h1> <!--/* [[${data}]] */--> <!--/*--> <span th:text="${data}">html data</span> <!--*/--> <h1>3. ํ์๋ฆฌํ ํ๋กํ ํ์ ์ฃผ์</h1> <!--/*/ <span th:text="${data}">html data</span> /*/--> </body> </html>
1. ๋ ๋๋ง์ ๋จ๊ฒจ๋์
2. ๋ ๋๋ง ์์ฒด๊ฐ ๋์ง ์์ <- ์ฃผ๋ก ์ฌ์ฉ
3. ์ ์ฐ์ง์์ ๋ณ ํ์์์ ํ์ผ์ ๊ทธ๋๋ก ์ด๋ฉด ์์(์ฃผ์ ์ฒ๋ฆฌ), ๋ ๋๋ง ํ์๋ ๋ณด์
โ ๋ธ๋ก
each๋ div ๋จ์์
ํ์ง๋ง div ๋ ๊ฐ์ฉ ๋ฐ๋ณต ๋๋ฆฌ๊ณ ์ถ์ ๋๋ ์์ -> ๊ทธ๋ด ๋ ๋ธ๋ก์ผ๋ก ๋ฌถ๋๋ค
๋ ๋๋ง์์๋ ์ฌ๋ผ์ง๊ณ div๋ง ๋จ์
@GetMapping("/block") public String block(Model model) { addUsers(model); return "basic/block"; }
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <th:block th:each="user : ${users}"> <div> ์ฌ์ฉ์ ์ด๋ฆ1 <span th:text="${user.username}"></span> ์ฌ์ฉ์ ๋์ด1 <span th:text="${user.age}"></span> </div> <div> ์์ฝ <span th:text="${user.username} + ' / ' + ${user.age}"></span> </div> </th:block> </body> </html>
โ ์๋ฐ์คํฌ๋ฆฝํธ ์ธ๋ผ์ธ
์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ ์ ์์
์ธ๋ผ์ธ ์ฌ์ฉ ์ : ๋ณ์ ์ฌ์ฉ์ -> " ํ์ํจ,
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <!-- ์๋ฐ์คํฌ๋ฆฝํธ ์ธ๋ผ์ธ ์ฌ์ฉ ์ --> <script> var username = [[${user.username}]]; var age = [[${user.age}]]; //์๋ฐ์คํฌ๋ฆฝํธ ๋ด์ถ๋ด ํ ํ๋ฆฟ var username2 = /*[[${user.username}]]*/ "test username"; //๊ฐ์ฒด var user = [[${user}]]; </script> <!-- ์๋ฐ์คํฌ๋ฆฝํธ ์ธ๋ผ์ธ ์ฌ์ฉ ํ --> <script th:inline="javascript"> var username = [[${user.username}]]; var age = [[${user.age}]]; //์๋ฐ์คํฌ๋ฆฝํธ ๋ด์ถ๋ด ํ ํ๋ฆฟ var username2 = /*[[${user.username}]]*/ "test username"; //๊ฐ์ฒด var user = [[${user}]]; </script> </body> </html>
@GetMapping("/javascript") public String javascript(Model model) { model.addAttribute("user", new User("userA", 10)); addUsers(model); return "basic/javascript"; }
โ ํ ํ๋ฆฟ ์กฐ๊ฐ
@Controller @RequestMapping("/template") public class TemplateController { @GetMapping("/fragment") public String template() { return "template/fragment/fragmentMain"; } }
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <body> <footer th:fragment="copy"> ํธํฐ ์๋ฆฌ ์ ๋๋ค. </footer> <footer th:fragment="copyParam (param1, param2)"> <p>ํ๋ผ๋ฏธํฐ ์๋ฆฌ ์ ๋๋ค.</p> <p th:text="${param1}"></p> <p th:text="${param2}"></p> </footer> </body> </html>
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>๋ถ๋ถ ํฌํจ</h1> <h2>๋ถ๋ถ ํฌํจ insert</h2> <div th:insert="~{template/fragment/footer :: copy}"></div> <h2>๋ถ๋ถ ํฌํจ replace</h2> <div th:replace="~{template/fragment/footer :: copy}"></div> <h2>๋ถ๋ถ ํฌํจ ๋จ์ ํํ์</h2> <div th:replace="template/fragment/footer :: copy"></div> <h1>ํ๋ผ๋ฏธํฐ ์ฌ์ฉ</h1> <div th:replace="~{template/fragment/footer :: copyParam ('๋ฐ์ดํฐ1', '๋ฐ์ดํฐ2')}"></div> </body> </html>
์๋ ์ฝ๋๋ก ์ ์ฝ๋๊ฐ ์ ์ ๋ค์ด๊ฐ๋ ํํ
โ ํ ํ๋ฆฟ ๋ ์ด์์
@GetMapping("/layout") public String layout() { return "template/layout/layoutMain"; }
<html xmlns:th="http://www.thymeleaf.org"> <head th:fragment="common_header(title,links)"> <title th:replace="${title}">๋ ์ด์์ ํ์ดํ</title> <!-- ๊ณตํต --> <link rel="stylesheet" type="text/css" media="all" th:href="@{/css/awesomeapp.css}"> <link rel="shortcut icon" th:href="@{/images/favicon.ico}"> <script type="text/javascript" th:src="@{/sh/scripts/codebase.js}"></script> <!-- ์ถ๊ฐ --> <th:block th:replace="${links}"/> </head>
์ด ๊ณณ์ head๋ฅผ ์๋ ์ฝ๋์์ ๊ฐ์ ธ๊ฐ๋ค.
title๊ณผ link๋ถ๋ถ์ title, link๊ฐ ๋งค๊ฐ๋ณ์๋ก ๋ค์ด๊ฐ
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head th:replace="template/layout/base :: common_header(~{::title},~{::link})"> <title>๋ฉ์ธ ํ์ดํ</title> <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}"> <link rel="stylesheet" th:href="@{/themes/smoothness/jquery-ui.css}"> </head> <body> ๋ฉ์ธ ์ปจํ ์ธ </body> </html>
replace = "${title}" ๋๋ฌธ์ ํ์ดํ์ ์๋ ์ฝ๋์์ ๊ฐ์ ธ๊ฐ๋ค.
replace = "${link}" ๋๋ฌธ์ link๋ ์๋ ์ฝ๋์์ ๊ฐ์ ธ๊ฐ๋ค.
ํ์ฅ ver
@GetMapping("/layoutExtend") public String layoutExtends() { return "template/layoutExtend/layoutExtendMain"; }
<!DOCTYPE html> <html th:fragment="layout (title, content)" xmlns:th="http://www.thymeleaf.org"> <head> <title th:replace="${title}">๋ ์ด์์ ํ์ดํ</title> </head> <body> <h1>๋ ์ด์์ H1</h1> <div th:replace="${content}"> <p>๋ ์ด์์ ์ปจํ ์ธ </p> </div> <footer> ๋ ์ด์์ ํธํฐ </footer> </body> </html>
์๋ ์ฝ๋์์ ์ html ํํฌ ํต์ผ๋ก ๊ฐ์ ธ๊ฐ
title๊ณผ content๋ถ๋ถ์ title, section์ด ๋งค๊ฐ๋ณ์๋ก ๋ค์ด๊ฐ
<!DOCTYPE html> <html th:replace="~{template/layoutExtend/layoutFile :: layout(~{::title},~{::section})}" xmlns:th="http://www.thymeleaf.org"> <head> <title>๋ฉ์ธ ํ์ด์ง ํ์ดํ</title> </head> <body> <section> <p>๋ฉ์ธ ํ์ด์ง ์ปจํ ์ธ </p> <div>๋ฉ์ธ ํ์ด์ง ํฌํจ ๋ด์ฉ</div> </section> </body> </html>
'SPRING > INFLEARN' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๊ฐ์] ์คํ๋ง MVC 2ํธ - ๋ฐฑ์๋ ์น ๊ฐ๋ฐ ํต์ฌ ๊ธฐ์ 5 (0) 2023.01.30 [๊ฐ์] ์คํ๋ง MVC 2ํธ - ๋ฐฑ์๋ ์น ๊ฐ๋ฐ ํต์ฌ ๊ธฐ์ 3 (0) 2023.01.13 [๊ฐ์] ์ค์ ! Querydsl 1 (0) 2022.08.22 [๊ฐ์] ์ค์ ! ์คํ๋ง ๋ฐ์ดํฐ JPA 1 (0) 2022.08.15 [๊ฐ์] ์ค์ ! ์คํ๋ง ๋ถํธ์ JPA ํ์ฉ2 - API ๊ฐ๋ฐ๊ณผ ์ฑ๋ฅ ์ต์ ํ 2 (0) 2022.08.02