Spring/SpringBoot

Spring Boot으로 웹 출시까지 #3. thymeleaf 기본 익히기

느리지만 꾸준하게 2022. 5. 30. 12:48
  • thymeleaf는  Spring Boot에서 권장하는 JSP를 대체하는 서버사이드 자바 템플릿 엔진

 

  • Spring Boot에서 JSP보다 더 간단한 설정과 HTML 표준 문법으로 thymeleaf를 이용해 HTML을 작성할 수 있다.

 

thymeleaf는 HTML 문법을 기반으로 한다.

<table>
  <thead>
    <tr>
      <th th:text="#{msgs.headers.name}">Name</th>
      <th th:text="#{msgs.headers.price}">Price</th>
    </tr>
  </thead>
  <tbody>
    <tr th:each="prod: ${allProducts}">
      <td th:text="${prod.name}">Oranges</td>
      <td th:text="${#numbers.formatDecimal(prod.price, 1, 2)}">0.99</td>
    </tr>
  </tbody>
</table>

 

thymeleaf tutorial에서  Using Texts를 참고해보자.

 

텍스트 사용에서 참고할 내용은 간단하다.

th 붙은 내용이 thymeleaf에서 구동이 된다.

href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">

  <head>
    <title>Good Thymes Virtual Grocery</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" media="all" 
          href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
  </head>

  <body>
  
    <p th:text="#{home.welcome}">Welcome to our grocery store!</p>
  
  </body>

</html>

 

 

 

4. Standard Expression Syntax

서버 기동일 때 달러 내용안에 값이 대체가 된다.

<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>

<p>Today is: <span th:text="${today}">13 february 2011</span></p>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<참고 :Spring Boot으로 웹 출시까지 #3. thymeleaf 기본 익히기>

https://www.youtube.com/watch?v=YWG_huG3Gr0&list=PLPtc9qD1979DG675XufGs0-gBeb2mrona&index=3