카테고리 없음

[Spring] Thymeleaf

321 2021. 6. 13. 11:58

https://www.thymeleaf.org/documentation.html

 

Documentation - Thymeleaf

Articles Quick glimpses into what Thymeleaf can bring to your project. Introductions With Spring Comparisons

www.thymeleaf.org

 

타임리프에서 제공하는 6가지 템플릿

  • html
  • xml
  • text
  • javascript
  • css
  • raw

 

표준 표현 syntax

  • ${... } : 변수 표현
    • ${book.user.name} --> EL(jsp)에서 이렇게 사용한 것을 <span th:text="${book.user.name}"> 이렇게 사용할 수 있다.
    • 또한 이터레이션을 사용할 때는 <li th:each="book : ${books}"> 로 사용가능하다.

 

  • *{... } : 선택 표현 - th:object를 사용해서 특정값을 받아올 때 사용함
    •  <div th:object="${book}">  ...  <span th:text="*{title}">...  <div>

 

  • #{... } : message - 프로퍼티 파일이나 파라미터 세트를 가져올 때 사용. 스프링의 messagesource 메커니즘을 따른다.
    • #{main.title} 
    • #{  message.entrycreated( ${entryId} )  }
    • 템플릿안에서는 <th th:text ="#{header.address.city}">... </th>와 같이 사용함
    •  

 

 

  • ~{ ... } : Fragment - th:insert, th:replace사용해서 사용가능
    • <div th:insert="~{commons :: main}">... </div>
    • <div th:with="frag=~{footer :: #main/text()}">  <p th:insert="${frag}">  </div>

 


 

Literals and operations

A good bunch of types of literals and operations are available:

  • Literals:
    • Text literals: 'one text', 'Another one!',…
    • Number literals: 0, 34, 3.0, 12.3,…
    • Boolean literals: true, false
    • Null literal: null
    • Literal tokens: one, sometext, main,…
  • Text operations:
    • String concatenation: +
    • Literal substitutions: |The name is ${name}|
  • Arithmetic operations:
    • Binary operators: +, -, *, /, %
    • Minus sign (unary operator): -
  • Boolean operations:
    • Binary operators: and, or
    • Boolean negation (unary operator): !, not
  • Comparisons and equality:
    • Comparators: >, <, >=, <= (gt, lt, ge, le)
    • Equality operators: ==, != (eq, ne)
  • Conditional operators:
    • If-then: (if) ? (then)
    • If-then-else: (if) ? (then) : (else)
    • Default: (value) ?: (defaultvalue)