01.Thymeleaf

Thymeleaf

๊ธฐ๋ณธ ๊ธฐ๋Šฅ

  • ์‚ฌ์šฉ ์„ ์–ธ

    <html xmlns:th="http://www.thymeleaf.org"></html>
  • ์†์„ฑ ๋ณ€๊ฒฝ

    th:href="@{/css/bootstrap.min.css}"
  • URL ๋งํฌ ํ‘œํ˜„์‹

    th:href="@{/css/bootstrap.min.css}"
    <!-- -->
    th:href="@{/basic/items/{itemId}(itemId=${item.id})}"
    th:href="@{/basic/items/{itemId}(itemId=${item.id}, query='test')}"
    th:href="@{|/basic/items/${item.id}|}"
  • ์†์„ฑ ๋ณ€๊ฒฝ

    th:onclick="|location.href='@{/basic/items/add}'|"
  • ๋ฐ˜๋ณต ์ถœ๋ ฅ

    <tr th:each="item : ${items}"></tr>
  • ๋ณ€์ˆ˜ ํ‘œํ˜„์‹

    <td th:text="${item.price}">10000</td>
  • ์†์„ฑ ๋ณ€๊ฒฝ

    <input
      type="text"
      id="price"
      name="price"
      value="10000"
      th:value="${item.price}"
    />

๊ธฐ๋ณธ ํ‘œํ˜„์‹

https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#standard-expression-syntax

ํ…์ŠคํŠธ

  • ํ…์ŠคํŠธ ์ถœ๋ ฅ

    • ๊ธฐ๋ณธ์ ์œผ๋กœ escape ๋ฅผ ์ œ๊ณต

      • escape : HTML ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ HTML ์—”ํ‹ฐํ‹ฐ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ

      • HTML ์—”ํ‹ฐํ‹ฐ : < ๋ฌธ์ž๋ฅผ ํƒœ๊ทธ์˜ ์‹œ์ž‘์ด ์•„๋‹Œ ๋ฌธ์ž๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•

  • Unescape

    • th:text --> th:utext

    • [[...]] --> [(...)]

SpringEL ํ‘œํ˜„์‹

Object

  • ${user.username} = userA

  • ${user['username']} = userA

  • ${user.getUsername()} = userA

List

  • ${users[0].username} = userA

  • ${users[0]['username']} = userA

  • ${users[0].getUsername()} = userA

Map

  • ${userMap['userA'].username} = userA

  • ${userMap['userA']['username']} = userA

  • ${userMap['userA'].getUsername()} = userA

Safe Navigation Operator

  • errors?. ์€ errors ๊ฐ€ null ์ผ๋•Œ NullPointerException ๋Œ€์‹ , null ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ๋ฒ• ์ฐธ๊ณ 

์ง€์—ญ๋ณ€์ˆ˜

๊ธฐ๋ณธ ๊ฐ์ฒด

Thymeleaf ๊ธฐ๋ณธ ๊ฐ์ฒด

  • ${#request}

  • ${#response}

  • ${#session}

  • ${#servletContext}

  • ${#locale}

ํŽธ์˜ ๊ฐ์ฒด

  • HTTP ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ ์ ‘๊ทผ: ${param.paramData}

  • HTTP ์„ธ์…˜ ์ ‘๊ทผ: ${session.sessionData}

  • ์Šคํ”„๋ง ๋นˆ ์ ‘๊ทผ: ${@helloBean.hello('Spring!')}

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

  • #message : ๋ฉ”์‹œ์ง€, ๊ตญ์ œํ™” ์ฒ˜๋ฆฌ

  • #uris : URI ์ด์Šค์ผ€์ดํ”„ ์ง€์›

  • #dates : java.util.Date ์„œ์‹ ์ง€์›

  • #calendars : java.util.Calendar ์„œ์‹ ์ง€์›

  • #temporals : ์ž๋ฐ”8 ๋‚ ์งœ ์„œ์‹ ์ง€์›

  • #numbers : ์ˆซ์ž ์„œ์‹ ์ง€์›

  • #strings : ๋ฌธ์ž ๊ด€๋ จ ํŽธ์˜ ๊ธฐ๋Šฅ

  • #objects : ๊ฐ์ฒด ๊ด€๋ จ ๊ธฐ๋Šฅ ์ œ๊ณต

  • #bools : boolean ๊ด€๋ จ ๊ธฐ๋Šฅ ์ œ๊ณต

  • #arrays : ๋ฐฐ์—ด ๊ด€๋ จ ๊ธฐ๋Šฅ ์ œ๊ณต

  • #lists , #sets , #maps : ์ปฌ๋ ‰์…˜ ๊ด€๋ จ ๊ธฐ๋Šฅ ์ œ๊ณต

  • #ids : ์•„์ด๋”” ์ฒ˜๋ฆฌ ๊ด€๋ จ ๊ธฐ๋Šฅ ์ œ๊ณต, ๋’ค์—์„œ ์„ค๋ช…

Reference

ํƒ€์ž„๋ฆฌํ”„ ์œ ํ‹ธ๋ฆฌํ‹ฐ ๊ฐ์ฒด

์œ ํ‹ธ๋ฆฌํ‹ฐ ๊ฐ์ฒด ์˜ˆ์‹œ

URL ๋งํฌ

  • ๋‹จ์ˆœ URL

    • /hello

  • query parameter

    • /hello?param1=data1&param2=data2

  • path variable

    • /hello/data1/data2

  • query parameter + path variable

    • /hello/data1?param2=data2

Reference

https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#link-urls

๋ฆฌํ„ฐ๋Ÿด

  • ๋ฌธ์ž: 'hello' (๋ฌธ์ž ๋ฆฌํ„ฐ๋Ÿด์€ ํ•ญ์ƒ ์ž‘์€๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ์•ผ ํ•จ)

  • ์ˆซ์ž: 10

  • ๋ถˆ๋ฆฐ: true , false

  • null: null

์—ฐ์‚ฐ

  • ์‚ฐ์ˆ  ์—ฐ์‚ฐ

  • ๋น„๊ต ์—ฐ์‚ฐ

  • ์กฐ๊ฑด์‹

  • Elvis ์—ฐ์‚ฐ์ž

  • No-Operation

์†์„ฑ ๊ฐ’ ์„ค์ •

  • ์†์„ฑ ์„ค์ •

  • ์†์„ฑ ์ถ”๊ฐ€

  • checked ์ฒ˜๋ฆฌ

๋ฐ˜๋ณต

  • ๋ฐ˜๋ณต

  • ์ƒํƒœ ์œ ์ง€

    • index : 0๋ถ€ํ„ฐ ์‹œ์ž‘

    • count : 1๋ถ€ํ„ฐ ์‹œ์ž‘

    • size : ์ „์ฒด ์‚ฌ์ด์ฆˆ

    • even , odd : ํ™€/์ง์ˆ˜ ์—ฌ๋ถ€

    • first , last :์ฒ˜์Œ/๋งˆ์ง€๋ง‰ ์—ฌ๋ถ€

    • current : ํ˜„์žฌ ๊ฐ์ฒด

์กฐ๊ฑด๋ถ€ ํ‰๊ฐ€

  • if, unless

  • switch

์ฃผ์„

  • ํ‘œ์ค€ HTML ์ฃผ์„

    • ํƒ€์ž„๋ฆฌํ”„๊ฐ€ ๋ Œ๋”๋งํ•˜์ง€ ์•Š๊ณ  ์œ ์ง€

  • ํƒ€์ž„๋ฆฌํ”„ ํŒŒ์„œ ์ฃผ์„

    • ๋ Œ๋”๋ง์—์„œ ์ฃผ์„ ๋ถ€๋ถ„์„ ์ œ๊ฑฐ (ํƒ€์ž„๋ฆฌํ”„ ์ฃผ์„)

  • ํƒ€์ž„๋ฆฌํ”„ ํ”„๋กœํ† ํƒ€์ž… ์ฃผ์„

    • ํƒ€์ž„๋ฆฌํ”„ ๋ Œ๋”๋ง์„ ๊ฑฐ์ณ์•ผ๋งŒ ์ด ๋ถ€๋ถ„์ด ์ •์ƒ ๋ Œ๋”๋ง (HTML ์—์„œ๋งŒ ์ฃผ์„ ์ฒ˜๋ฆฌ)

๋ธ”๋ก

  • th:each ๋กœ ํ•ด๊ฒฐ์ด ์–ด๋ ค์šธ ๋•Œ ์‚ฌ์šฉ

JavaScript Inline

  • javascript inline

  • each

ํ…œํ”Œ๋ฆฟ ์กฐ๊ฐ

  • /resources/templates/template/fragment/footer.html

  • /resources/templates/template/fragment/fragmentMain.html

ํ…œํ”Œ๋ฆฟ ๋ ˆ์ด์•„์›ƒ

๋™์ ์ธ ๋ ˆ์ด์•„์›ƒ

  • /resources/templates/template/layout/base.html

    • ๋ ˆ์ด์•„์›ƒ์ด๋ผ๋Š” ํฐ ํ‹€

  • /resources/templates/template/layout/layoutMain.html

    • ํ‹€ ์•ˆ์— ํ•„์š”ํ•œ ์ฝ”๋“œ ์กฐ๊ฐ๋“ค์„ ์ „๋‹ฌ

    • ::title ์€ ํ˜„์žฌ ํŽ˜์ด์ง€์˜ title tag ๋“ค์„ ์ „๋‹ฌ

    • ::link ์€ ํ˜„์žฌ ํŽ˜์ด์ง€์˜ link tag ๋“ค์„ ์ „๋‹ฌ

๋ฉ”์ธ ๋ ˆ์ด์•„์›ƒ

  • /resources/templates/template/layoutExtend/layoutFile.html

    • ๊ธฐ๋ณธ ๋ ˆ์ด์•„์›ƒ(header, footer) ํ‹€์€ ์œ ์ง€ํ•˜๊ณ  title, content ๋งŒ ๋ณ€๊ฒฝ

  • /resources/templates/template/layoutExtend/layoutExtendMain.html

    • ๊ธฐ๋ณธ ๋ ˆ์ด์•„์›ƒ ํ‹€๋กœ ๊ต์ฒดํ•˜๋Š”๋ฐ ํ•˜๋Š”๋ฐ title, content ๋Š” ์ „๋‹ฌ

Form

์ž…๋ ฅ ํผ ์ฒ˜๋ฆฌ

  • th:object : ์ปค๋งจ๋“œ ๊ฐ์ฒด๋ฅผ ์ง€์ •

  • \*{...} : ์„ ํƒ ๋ณ€์ˆ˜ ์‹ (th:object ์—์„œ ์„ ํƒํ•œ ๊ฐ์ฒด์— ์ ‘๊ทผ)

  • th:field : HTML ํƒœ๊ทธ์˜ id , name , value ์†์„ฑ์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑ

  • ๋ Œ๋”๋ง ์ „/ํ›„

์ฒดํฌ ๋ฐ•์Šค

๋‹จ์ผ

Register

  • ํƒ€์ž„๋ฆฌํ”„๊ฐ€ ์ž๋™์œผ๋กœ <input type="hidden" name="_open" value="on"/> ์ƒ์„ฑ

    • ์ฒดํฌ ๋ฐ•์Šค๋ฅผ ์ฒดํฌํ•  ๊ฒฝ์šฐ on ์„ ์ „๋‹ฌํ•˜์ง€๋งŒ, ์ฒดํฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ๊ฐ’ ์ž์ฒด๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์Œ -> ์ด ๊ฒฝ์šฐ hidden type ์˜ _name input ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด, false ๋กœ ๊ฐ’์„ ์ „๋‹ฌ (_open=on)

View

๋ฉ€ํ‹ฐ

(์ฐธ๊ณ ) @ModelAttribute ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น Controller ํ˜ธ์ถœ ์‹œ regions() ์—์„œ ๋ฐ˜ํ™˜ํ•œ ๊ฐ’์ด ์ž๋™์œผ๋กœ Model์— ํ•ญ์ƒ ๋‹ด๊ธฐ๊ฒŒ ๋จ

Register

  • ํƒ€์ž„๋ฆฌํ”„๋Š” each๋กœ ์ฒดํฌ๋ฐ•์Šค ์ƒ์„ฑ ์‹œ ๋™์ ์œผ๋กœ id์— ์ˆœ๋ฒˆ์„ ๋งค๊ฒจ์ค€๋‹ค. #ids๋Š” ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋œ id๋ฅผ ์ธ์‹

  • result

View

  • ํƒ€์ž„๋ฆฌํ”„๋Š” th:field์— ์ง€์ •ํ•œ ๊ฐ’๊ณผ th:value์˜ ๊ฐ’์„ ๋น„๊ตํ•ด์„œ ์ฒดํฌ๋ฅผ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌ

๋ผ๋””์˜ค ๋ฒ„ํŠผ

  • ๋ผ๋””์˜ค ๋ฒ„ํŠผ์€ ํ•ญ์ƒ ํ•˜๋‚˜์˜ ๊ฐ’์ด ์„ ํƒ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ ํžˆ๋“  ๋ฒ„ํŠผ์„ ๋”ฐ๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š์Œ.

์…€๋ ‰ํŠธ ๋ฐ•์Šค

Register

Reference

๊ณต์‹ ์‚ฌ์ดํŠธ

๊ณต์‹ ๋ฉ”๋‰ด์–ผ - ๊ธฐ๋ณธ ๊ธฐ๋Šฅ

๊ณต์‹ ๋ฉ”๋‰ด์–ผ - ์Šคํ”„๋ง ํ†ตํ•ฉ

Last updated