ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๊ฐ•์˜] ์Šคํ”„๋ง MVC 2ํŽธ - ๋ฐฑ์—”๋“œ ์›น ๊ฐœ๋ฐœ ํ•ต์‹ฌ ๊ธฐ์ˆ  1
    SPRING/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> &lt;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>

    โ— ์œ ํ‹ธ๋ฆฌํ‹ฐ ๊ฐ์ฒด์™€ ๋‚ ์งœ

    ๋งŽ๊ณ  ๊ฐ„๋‹จํ•ด์„œ ๊ทธ๋ƒฅ ์ฐธ๊ณ 

    https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#appendix-b-expression-utility-objects

     

    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 &gt; 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>

Designed by Tistory.