规范表达式语法

  • ${...}:变量表达式
  • *{...}:抉择表达式
  • \#{...}:音讯(i18n)表达式
  • @{...}:URL表达式
  • ~{...}:片段表达式

变量表达式

将Thymeleaf与Spring集成-在上下文变量(在Spring行话中也称为 Spring jargon)上执行

OGNL表达式长这样 :

${session.user.name}

他们能够作为属性的值,像这样:

<span th:text="${book.author.name}">

以上代码和这样是相等的(OGNL和SpEL语言)

((Book)context.getVariable("book")).getAuthor().getName()

咱们能够应用迭代拜访变量

<li th:each="book : ${books}">

$ {books}从上下文中抉择称为books的变量,并将其评估为可迭代的变量,以在th:each循环中应用

抉择表达式

抉择表达式和变量表达式相似,而抉择表达式只能在先前抉择的对象上执行,不能全局执行
抉择表达式长这样:

*{customer.name}

它们作用的对象由th:object属性指定

<div th:object="${book}">  ...  <span th:text="*{title}">...</span>  ...</div>

等效于:

{  // th:object="${book}"  final Book selection = (Book) context.getVariable("book");  // th:text="*{title}"  output(selection.getTitle());}

音讯表达式

常常称为text externalization, internationalization或者 i18n
音讯表达式容许咱们从.properties文件中检索特定于语言环境的音讯,通过键援用它们,并可选地利用一组参数。
他们长这样:

#{main.title}
#{message.entrycreated(${entryId})}

在模板中长这样:

<table>  ...  <th th:text="#{header.address.city}">...</th>  <th th:text="#{header.address.country}">...</th>  ...</table>

如果心愿音讯关键字由上下文变量的值确定,或者心愿将变量指定为参数。
能够在音讯表达式内应用变量表达式,像这样:

#{${config.adminWelcomeKey}(${session.user.name})}

链接表达式

链接表达式用于构建URL并向其增加有用的上下文和会话信息(此过程通常称为URL重写)
一个网页利用部署在你的web服务器的myapp上下文,表达式长这样:

<a th:href="@{/order/list}">...</a>

他会被转换为:

<a href="/myapp/order/list">...</a>

如果咱们须要保留会话且未启用Cookie(或服务器尚不晓得),请执行以下操作

<a href="/myapp/order/list;jsessionid=23fa31abd41ea093">...</a>

URL还能够退出变量

<a th:href="@{/order/details(id=${orderId},type=${orderType})}">...</a>

他理论是会被转换为这样的:

<!-- Note ampersands (&) should be HTML-escaped in tag attributes... --><a href="/myapp/order/details?id=23&amp;type=online">...</a>

URL表达式能够是绝对的,在这种状况下,没有应用程序上下文将作为URL的前缀:

<a th:href="@{../documents/report}">...</a>

也能够是服务器的绝对(同样也没有URL前缀)

<a th:href="@{~/contents/main}">...</a>

绝对协定(与相对URL雷同,但浏览器将应用与所显示页面雷同的HTTP或HTTPS协定):

<a th:href="@{//static.mycompany.com/res/initial}">...</a>

url表达式也当然能够是相对的

<a th:href="@{http://www.mycompany.com/main}">...</a>

片段表达式

片段表达式是示意标记片段并在模板中应用它们的简略办法。
最常见的用处是应用th:insert或th:replace插入片段:

<div th:insert="~{commons :: main}">...</div>

能够在任何中央应用,像变量一样

<div th:with="frag=~{footer :: #main/text()}">  <p th:insert="${frag}"></div>

字面量和办法

  • 字面量:

    * 文本字面量:'one text', 'Another one!'* 数字字面量:0, 34, 3.0, 12.3,* 布尔字面量:true,false* 空关字面量:null
  • 文本操作:

    *   String concatenation: +*   Literal substitutions: |The name is ${name}|
  • 数学操作:

    *   Binary operators: +, -, *, /, %*   Minus sign (unary operator): -
  • 布尔操作符:

    *   Binary operators: and, or*   Boolean negation (unary operator): !, not
  • 比拟和相等:

    *   Comparators: >, <, >=, <= (gt, lt, ge, le)*   Equality operators: ==, != (eq, ne)
  • 条件操作:

    *   If-then: (if) ? (then)*   If-then-else: (if) ? (then) : (else)*   Default: (value) ?: (defaultvalue)

表达式预处理

预处理看起来像这样

#{selection.__${sel.code}__}

咱们看到的是一个表达式${sel.code},他将被事后执行,而后将其后果(如果是ALL)selection.ALL返回

一些根本属性

th:text替换标签的文本

<p th:text="#{msg.welcome}">Welcome everyone!</p>

th:each和Java的forEach雷同

<li th:each="book : ${books}" th:text="${book.title}">En las Orillas del Sar</li>

Thymeleaf模拟的特定的XHTML和HTML5属性th:action, th:value, th:href

<form th:action="@{/createOrder}">
<input type="button" th:value="#{form.submit}" />
<a th:href="@{/admin/users}">