#author("2019-11-15T14:00:46+08:00","default:Admin","Admin") Thymeleaf 在Thymeleaf 中,我们可以使用th:fragment属性来定义一个模板。 我们可以新建一个简单的页尾模板,如:/WEB-INF/templates/footer.html,内容如下: <!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <body> <div th:fragment="copyright"> © 2016 xxx </div> </body> </html> 上面定义了一个叫做copyright的片段,接着我们可以使用th:include或者th:replace属性来使用它: <body> ... <div th:include="footer :: copyright"></div> </body> 模板片段可以被包含在任意th:*属性中,并且可以使用目标页面中的上下文变量。 th:include 和 th:replace区别 †th:include 是加载模板的内容,而th:replace则会替换当前标签为模板中的标签 例如如下模板: <footer th:fragment="copy"> © 2016 </footer> 我们通过th:include 和 th:replace来加载模板 <body> <div th:include="footer :: copy"></div> <div th:replace="footer :: copy"></div> </body> 返回的HTML如下: <body> <div> © 2016 </div> <footer>© 2016 </footer> </body> 超多機能のアクセス解析が永久無料! コメント: |