MapleStory Cookie With Halo

โœจ UI/๐Ÿ–ค View Templates

[Thymeleaf]ํƒ€์ž„๋ฆฌํ”„ ์‚ฌ์šฉ๋ฒ• ๊ฐ„๋‹จ์ •๋ฆฌ

๋‰ด์ด NUEY 2025. 3. 3. 19:40
๋ฐ˜์‘ํ˜•

 

 

 

  • ํƒ€์ž„๋ฆฌํ”„ ์ ์šฉ
    <html lang="ko" xmlns:th="http://www.thymeleaf.org">
    • code 

       
    •  HTML์— ํƒ€์ž„๋ฆฌํ”„๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค. HTML์„ ํ™œ์šฉํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— natural template์ด๋ผ๊ณ  ํ•œ๋‹ค.

    • th ๋ผ๋Š” ์•ฝ์–ด๋กœ ํƒ€์ž„๋ฆฌํ”„๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

  • ์‚ฌ์šฉ๋ฒ•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

๋ฐ˜์‘ํ˜•