Thymeleaf表达式
1、${} 选择表达式(星号表达式)
字符串处理
<span th:text="'00:00/'+${totalTime}">
静态内容用单引号''包裹起来,和上下文变量之间用+连接。
字符串拼接优化
Thymeleaf做字符串拼接还做了优化工作,我们可以使用上面的代码你还可以这样|围住字符串,这样就不需要在文字后面附加'...'+'...'
<span th:text="|00:00/${totalTime}|">
数据转化
Thymeleaf默认集成了大量的工具类可以方便的进行数据转化,一我们使用最多的是dates
@RequestMapping("/demo")
public String index(Model model){
Date dateVar = new Date();
model.addAttribute("dateVar",dateVar);
return "demo";
}
前台显示:
//显示年月日
<p th:text="${#dates.format(dateVar, 'yyyy-MM-dd')}"></p>
<p th:text="${#dates.format(dateVar, 'yyyy年MM月dd日')}"></p>
//显示年月日时分秒
<p th:text="${#dates.format(dateVar, 'yyyy-MM-dd HH:mm:ss')}"></p>
<p th:text="${#dates.format(dateVar, 'yyyy年MM月dd日 HH时mm分ss秒')}"></p>
在这里如果后台的日期类型是LocalDate/LocalDateTime,那么就把#date换成#temporals
注意:需要在pom.xml文件里添加依赖
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
这个库会自动添加一个新的工具类temporals
工具类的运用和变量不同,变量使用的是${变量名} ,工具类使用的是#{工具类}。
后台代码:
@RequestMapping("/demo")
public String index(Model model){
LocalDateTime dateVar = LocalDateTime.now();
model.addAttribute("dateVar",dateVar);
return "demo";
}
前台代码:
//显示年月日
<p th:text="${#temporals.format(dateVar, 'yyyy-MM-dd')}"></p>
<p th:text="${#temporals.format(dateVar, 'yyyy年MM月dd日')}"></p>
````html
//显示年月日时分秒
````html
<p th:text="${#temporals.format(dateVar, 'yyyy-MM-dd HH:mm:ss')}"></p>
<p th:text="${#temporals.format(dateVar, 'yyyy年MM月dd日 HH时mm分ss秒')}"></p>
dates/temporals工具类说明
dates和temporals支持的方法是一样的, 只是支持的类型不同,dates支持的是Date类, temporals支持的是LocalDate 和LocalDateTime。
java.util.Date类和LocalDateTime类功能是一样的,不同的是LocalDateTime是Java8才出现的,一老的应用还是用Date类的。
strings工具类
Thymeleaf里的其他工具类
各内置对象包含哪些方法可以点此看文档,也可以点这里看官方文档。
Thymeleaf内联表达式
其实就是th:text的另一种写法,直接把变量写在HTML中,格式:[[变量]]
<span>Hello [[${msg}]]</span>
<p>[[ ${#dates.format(dateVar, 'yyyy-MM-dd')} ]]</p>
<p>[[${#dates.format(dateVar, 'yyyy年MM月dd日')}]]</p>
<p>[[${#dates.format(dateVar, 'yyyy-MM-dd HH:mm:ss')}]]</p>
<p>[[${#dates.format(dateVar, 'yyyy年MM月dd日 HH时mm分ss秒')}]]</p>
注意:
内联表达式[[]]只能替代th:text不是替代所有的th:标签。
2、*{} 选择表达式(星号表达式)
选择表达式与变量表达式有一个重要的区别:选择表达式计算的是选定的对象,而不是整个环境变量映射。也就是:只要是没有选择的对象,选择表达式与变量表达式的语法是完全一样的。那什么是选择的对象呢?是一个:th:object
对象属性绑定的对象。
<div th:object=" ${session.user}" >
<p>Name: <span th: text=" *{firstName}" >Sebastian</span>. </p>
<p>Surname: <span th: text=" *{lastName}" >Pepper</span>. </p>
<p>Nationality: <span th: text=" *{nationality}" >Saturn</span>. </p>
</div>
上例中,选择表达式选择的是th:object对象属性绑定的session. user对象中的属性。
3、#{} 消息表达式(井号表达式,资源表达式)
通常与th:text属性一起使用,指明声明了th:text的标签的文本是#{}中的key所对应的value,而标签内的文本将不会显示。
例如:
新建/WEB-INF/templates/home.html,段落
<p th: text="#{home.welcome}" >This text will not be show! </p>
新建/WEB-INF/templates/home.properties,home.welcome:
home.welcome=this messages is from home.properties!
测试结果:
从测试结果可以看出,消息表达式通常用于显示页面静态文本,将静态文本维护在properties文件中也方便维护,做国际化等。
4、@{} 超链接url表达式
<script th:src="@{/resources/js/jquery/jquery.json-2.4.min.js}"/>
5、#maps
工具对象表达式。常用于日期、集合、数组对象的访问。这些工具对象就像是java对象,可以访问对应java对象的方法来进行各种操作。用法在上面数据转化已经举例
- dates
- calendars
- numbers
- strings
- objects
- bools
- arrays
- lists
- sets