内嵌变量(Utility对象)
使用:"${#对象.方法(参数列表)}"
dates : java.util.Date 的功能方法类
numbers : 格式化数字的功能方法
strings : 字符串对象的功能类
calendars : 类似#dates,面向java.util.Calendar
objects: 对objects的功能类操作
bools: 对布尔值求值的功能方法
- 例.日期格式化(schooltime为后台的Date对象值)
<div th:text="${#dates.format(schooltime,'yyyy-MM-dd HH:mm:ss')}"></div >
- 例.字符串拼接
<div th:text="${#strings.append('hello ','world')}"></div> //打印hello world
- 例.使用strings生成随机字符串(2为字符串的长度)
<div th:text="${#strings.randomAlphanumeric(2)}"></div>
thymeleaf - 布局th:include/th:replace
th:include 加载模块的内容,覆盖原标签的内容
th:replace 替换原标签及内容
th:fragment 创建模板。
例子:
页面 hello.html 中th:fragment创建模板
页面 hello2.html 写个div
hello.html
<div th:fragment="f1"> f1 </div>
<div th:fragment="f2"> f2 </div>
hello2.html
<div style="border: solid 1px red;">hello2页面div</div>
打开两个页面
分别显示:
测试th:include
hello2.html
<div th:include="hello"></div>
<div th:include="hello::f1" style="border: solid 1px red;">hello2页面div</div>
<!-- hello (指整个hello.html内容)
hello::f (指hello.html中带有 'th:fragment="f1"' 属性的标签及标签内容) -->
注意:访问hello2.html页面使用后台跳转方式
@RequestMapping("/hello")
public String hello(Model model){
return "hello2";
}
浏览器访问 http://localhost:8080/hello
测试th:replace
hello2.html
<div th:include="hello"></div>
<div th:include="hello::f1" style="border: solid 1px red;">hello2页面div</div>
浏览器访问 http://localhost:8080/hello