Getting started with the Standard dialects in 5 minutes

简介: 文章总结语法:${...} : Variable expressions.*{...} : Selection expressions.#{...} : Message (i18n) expressions.

文章总结
语法:

${...} : Variable expressions.
*{...} : Selection expressions.
#{...} : Message (i18n) expressions.
@{...} : Link (URL) expressions.
~{...} : Fragment expressions.

例子:

2.1 Variable expressions

取 session 中的变量:
1. ${session.user.name}
2. <span th:text="${book.author.name}">

更复杂应用:
3. <li th:each="book : ${books}">

2.2 Selection expressions

Variable expressions 类似,
they will be executed on a previously selected object instead of the whole context variables map

示例:

1. *{customer.name}

2. 注意使用 th:object 标签
<div th:object="${book}">
  ...
  <span th:text="*{title}">...</span>
  ...
</div>

2.3 Message(i18n)expressions

国际化示例:

1. #{main.title}
2. #{message.entrycreated(${entryId})}
3. 模板中使用:
<table>
  ...
  <th th:text="#{header.address.city}">...</th>
  <th th:text="#{header.address.country}">...</th>
  ...
</table>
4. 高级用法:在 message expressions 中使用 variable expressions
#{${config.adminWelcomeKey}(${session.user.name})}

2.4 Link(URL)expressions

原文翻译:给 url 添加上 context 和 session 信息
Link expressions are meant to build URLs and add useful context and session info to them (a process usually called URL rewriting).

示例:
So for a web application deployed at the /myapp context of your web server, an expression such as:

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

Could be converted into something like this:

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

Or even this, if we need to keep sessions and cookies are not enabled (or the server doesn’t know yet):

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

URLs can also take parameters:

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

Resulting in something like this:

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

Link expressions can be relative, in which case no application context will be prefixed to the URL:

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

Also server-relative (again, no application context to be prefixed):

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

And protocol-relative (just like absolute URLs, but browser will use the same HTTP or HTTPS protocol used in the page being displayed):

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

And of course, Link expressions can be absolute:

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

But wait, in an absolute (or protocol-relative) URL… what value does the Thymeleaf Link Expression add? easy: the possibility of URL-rewriting defined by response filters: In a Servlet-based web application, for every URL being output (context-relative, relative, absolute…) Thymeleaf will always call the HttpServletResponse.encodeUrl(...) mechanism before displaying the URL. Which means that a filter can perform customized URL-rewriting for the application by means of wrapping the HttpServletResponse object (a commonly used mechanism).

2.5 Fragment expressions

最常用的两个属性:
th:insert or th:replace

示例:

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

Fragment expressions can have arguments:

2.5 Expression preprocessing

格式:specified between __
示例:#{selection.${sel.code}}

说明(未懂):What we are seeing there is a variable expression (${sel.code}) that will be executed first and which result – let’s say, “ALL” – will be used as a part of the real expression to be executed afterwards, in this case an internationalization one (which would look for the message with key selection.ALL).

目录
相关文章
|
存储 Java
【JavaSE】基础笔记 - 图书管理系统(保姆教程,含源码)
【JavaSE】基础笔记 - 图书管理系统(保姆教程,含源码)
401 1
|
9月前
|
存储 Java
【潜意识Java】期末考试可能考的选择题(附带答案解析)
本文整理了 Java 期末考试中常见的选择题,涵盖数据类型、控制结构、面向对象编程、集合框架、异常处理、方法、流程控制和字符串等知识点。每道题目附有详细解析,帮助考生巩固基础,加深理解。通过这些练习,考生可以更好地准备考试,掌握 Java 的核心概念和语法。
385 1
|
测试技术 C# 开发者
“代码守护者:详解WPF开发中的单元测试策略与实践——从选择测试框架到编写模拟对象,全方位保障你的应用程序质量”
【8月更文挑战第31天】单元测试是确保软件质量的关键实践,尤其在复杂的WPF应用中更为重要。通过为每个小模块编写独立测试用例,可以验证代码的功能正确性并在早期发现错误。本文将介绍如何在WPF项目中引入单元测试,并通过具体示例演示其实施过程。首先选择合适的测试框架如NUnit或xUnit.net,并利用Moq模拟框架隔离外部依赖。接着,通过一个简单的WPF应用程序示例,展示如何模拟`IUserRepository`接口并验证`MainViewModel`加载用户数据的正确性。这有助于确保代码质量和未来的重构与扩展。
642 0
|
程序员 开发工具 git
|
关系型数据库 MySQL Unix
logging模块介绍
logging模块介绍
|
安全 5G 网络安全
5 分钟搞懂 5G 安全增强
5 分钟搞懂 5G 安全增强
398 0
|
存储 安全 数据安全/隐私保护
13 Tornado - Cookie
13 Tornado - Cookie
110 2
|
机器学习/深度学习 设计模式 JavaScript
|
存储 负载均衡 调度
LVS的DR模型实战应用|学习笔记
快速学习LVS的DR模型实战应用
LVS的DR模型实战应用|学习笔记
|
数据采集 存储 JSON
数据预处理-系统监控-监控代码下|学习笔记
快速学习数据预处理-系统监控-监控代码下
133 0
数据预处理-系统监控-监控代码下|学习笔记