我的thymeleaf语法总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014155085/article/details/79175071

一、常用语法

1.th:text 替换文本
<p th:text="${x.a}">被替换文本</p>
2.字符串拼接
<p th:text="${'拼接块'+x.a+'拼接块'+x.b+'拼接块'}" >被替换文本</p>
3.遍历List
<tr th:each="x:${xList}">
   <th th:text="${x.a}">被替换文本</th>
   <th th:text="${x.b+' '+x.c}" >被替换文本</th>
</tr>
4.格式化日期
th:text="${#dates.format(x.y, 'yyyy-MM-dd HH:mm:ss')}"


二、引入外部文件:

1.@{}的方式引入外部文件会包含项目名
css:    th:href="@{/css/***.css}"
js:     th:src="@{/js/***.js}"
img:    th:src="@{/img/***.png}"

2.include引入文件

(1)模板文件引入layout,并且声明decorator=“自定义X”

<html xmlns:th="http://www.thymeleaf.org" 
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" 
layout:decorator="web-model">

(2)模板文件写入内容,th:fragment=“自定义Y”。ps:th:remove=“tag”为了删掉外边包围着的div

<div th:fragment="left" th:remove="tag">
 ***模板内容***
</div>

(3)需要引入的模板文件的html

 <div th:include="web-model(自定义X)::left(自定义Y)" ></div>

(4)结果

“<div th:include="web-model(即自定义X)::left(即自定义Y)" ></div>”   替换成   “***模板内容***”






猜你喜欢

转载自blog.csdn.net/u014155085/article/details/79175071