@ruoli
2017-06-09T08:35:34.000000Z
字数 1633
阅读 3719
Web前端
##
模板代码片段,就在本页面中,示例代码如下:
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"><head><meta content="text/html;charset=UTF-8" /></head><body><!-- 此处是模版 --><div th:fragment="copy">此处是模版</div><!-- 此处调用模版 --><div th:include="::copy"></div></body></html>
#
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"><head><meta content="text/html;charset=UTF-8" /></head><body><!-- 此处是模版 --><div th:fragment="copy(onevar,twovar)"><p th:text="${onevar} + ' - ' + ${twovar}">...</p></div><!-- 此处调用模版,共有如下两种方式 --><div th:include="::copy('ee','yy')"></div><div th:include="::copy(onevar='bb',twovar='aa')"></div></body></html>
定义模板 footer.html,示例如下:
<div th:fragment="copy">© 2016 CodeApe.cn</div><div id="copy_1">基于ID属性的模板</div>
定义调用文件,示例如下:
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"><head><meta content="text/html;charset=UTF-8" /></head><body><!-- 此处调用模版,如下两种方式 --><!-- 基于模版中th:fragment属性 --><div th:include="footer::copy"></div><!-- 基于模版中Dom选择器 --><div th:include="footer::#copy_1"></div></body></html>
上述最后一种,基于DOM选择器的模板引用,调用格式:th:include="templatename::domselector"
templatename是要引入页面的路劲加上去掉后缀的名称,例如footer.html页面建立在/WEB-INF/templates/footer.html,所以templatename为footer;domselector就是dom选择器,即为th:fragment中的值,或是选择id
<tr th:each="model,state:${list}"><td th:text="${state.index+1}"></td><td th:text="${model.name}"></td><td th:text="${model.packageName}"></td></tr>