标准语法

  • ${…} : Variable expressions.

  • *{…} : Selection expressions.

  • {…} : Message (i18n) expressions.

  • @{…} : Link (URL) expressions.

  • ~{…} : Fragment expressions.

Variable expressions

  1. ${session.user.name}
  2. <span th:text="${book.author.name}">
  3. ((Book)context.getVariable("book")).getAuthor().getName()
  4. <li th:each="book : ${books}">
  5. //Here ${books} selects the variable called books from the context(环境), and evaluates(评价) it as an iterable to be used at a th:each loop.

Selection expressions

  1. *{customer.name}
  2. <div th:object="${book}">
  3. ...
  4. <span th:text="*{title}">...</span>
  5. ...
  6. </div>
  7. {
  8. // th:object="${book}"
  9. final Book selection = (Book) context.getVariable("book");
  10. // th:text="*{title}"
  11. output(selection.getTitle());
  12. }

Message (i18n) expressions

i18n:国际化

  1. #{main.title}
  2. #{message.entrycreated(${entryId})}
  3. <table>
  4. ...
  5. <th th:text="#{header.address.city}">...</th>
  6. <th th:text="#{header.address.country}">...</th>
  7. ...
  8. </table>
  9. #{${config.adminWelcomeKey}(${session.user.name})}

Link (URL) expressions

  1. <a th:href="@{/order/list}">...</a>
  2. <a href="/myapp/order/list">...</a>
  3. <a href="/myapp/order/list;jsessionid=23fa31abd41ea093">...</a>
  4. <a th:href="@{/order/details(id=${orderId},type=${orderType})}">...</a>
  5. <!-- Note ampersands (&) should be HTML-escaped in tag attributes... -->
  6. <a href="/myapp/order/details?id=23&amp;type=online">...</a>
  7. <!--relative url-->
  8. <a th:href="@{../documents/report}">...</a>
  9. <a th:href="@{~/contents/main}">...</a>
  10. <!--absolute(绝对的) URLs-->
  11. <a th:href="@{//static.mycompany.com/res/initial}">...</a>
  12. <a th:href="@{http://www.mycompany.com/main}">...</a>

Fragment expressions

  1. <div th:insert="~{commons :: main}">...</div>
  2. <div th:with="frag=~{footer :: #main/text()}">
  3. <p th:insert="${frag}">
  4. </div>

Literals and operations

  1. 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,…

  1. Text operations:
  • String concatenation: +

  • Literal substitutions: |The name is ${name}|

  1. Arithmetic operations:
  • Binary operators: +, -, *, /, %

  • Minus sign (unary(一元的) operator): -

  1. Boolean operations:
  • Binary operators: and, or

  • Boolean negation(否定) (unary operator): !, not

  1. Comparisons and equality:
  • Comparators: >, <, >=, <= (gt, lt, ge, le)

  • Equality operators: ==, != (eq, ne)

  1. Conditional operators:
  • If-then: (if) ? (then)

  • If-then-else: (if) ? (then) : (else)

  • Default: (value) ?: (defaultvalue)

基本属性

  1. th:text
  1. <p th:text="#{msg.welcome}">Welcome everyone!</p>
  1. th:each
  1. <li th:each="book : ${books}" th:text="${book.title}">En las Orillas del Sar</li>
  1. 自定义标签
  1. <form th:action="@{/createOrder}">
  2. <input type="button" th:value="#{form.submit}" />
  3. <a th:href="@{/admin/users}">
  1. 丹丹

参考链接

  1. Getting started with the Standard dialects in 5 minutes

  2. The Thymeleaf Interactive Tutorial

  3. thymeleaf-cheat-sheet

  4. spring boot 项目demo