thymeleaf基本语法

thymeleaf标准表达式语法

  • $ {…} :变量表达式。
  • *{…} :选择表达式。
  • #{…} :消息(i18n)表达式。
  • @ {…} :链接(URL)表达式。
  • ~ {…} :分段表达式。
  1. 变量的表达式
  • 官网解释:

Variable expressions are OGNL expressions –or Spring EL if you’re integrating Thymeleaf with Spring– executed on the context variables — also called model attributes in Spring jargon. They look like this:

  • 语法
${session.user.name}
  • 用法
<span th:text="${book.author.name}">

等效于

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

实际例子
在这里插入图片描述

  1. 选择表达式
  • 官网解释:

Selection expressions are just like variable expressions, except they will be executed on a previously selected object instead of the whole context variables map. They look like this:

  • 语法
*{customer.name}
  • 用法
<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());
}

实际例子
在这里插入图片描述

  1. 消息表达式
  • 官网解释:

Message expressions (often called text externalization, internationalization or i18n) allows us to retrieve locale-specific messages from external sources (.properties files), referencing them by a key and (optionally) applying a set of parameters.

即:
表达式(通常称为消息 文字外化 , 国际化 或 i18n )允许我们从外部来源(检索特定于本地语言环境的消息 . properties 文件),引用他们的一个关键和(可选)应用一组参数。

  • 语法
#{main.title}
#{message.entrycreated(${entryId})}
  • 用法
<table>
  ...
  <th th:text="#{header.address.city}">...</th>
  <th th:text="#{header.address.country}">...</th>
  ...
</table>

实际中的应用
在这里插入图片描述

  1. 链接(URL)表达式
  • 官网解释:

Message expressions (often called text externalization, internationalization or i18n) allows us to retrieve locale-specific messages from external sources (.properties files), referencing them by a key and (optionally) applying a set of parameters.

即:
链接表达式是用来构建url并添加有用的上下文和会话信息(通常被称为过程 URL重写 )。

所以对于一个web应用程序的部署 / myapp 您的web服务器的一个表达式,如:。

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

或者

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

还可以这样

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

相对地址,在这种情况下,没有应用程序上下文将前缀到URL:

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

于http协议相关(就像绝对url,但浏览器将使用相同的HTTP或HTTPS协议用于页面显示):

<a th:href="@{//static.mycompany.com/res/initial}">...</a>
  1. 分段表达式
  • 官网解释:

Fragment expressions are an easy way to represent fragments of markup and move them around templates. Thanks to these expressions, fragments can be replicated, passed to other templates as arguments, and so on.

即:
片段表达式是一个简单的方法来表示标记的片段和移动模板。 由于这些表达式,碎片可以被复制,作为参数传递给其他模板,等等。

最常见的使用是片段插入使用 th:插入 或 th:替换 :

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

但他们可以在任何地方使用,就像任何其他变量:

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

实际中应用
在这里插入图片描述
相当于在下面的页面中应用上面那段html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值