【发布时间】:2016-09-03 09:23:14
【问题描述】:
两个Thymeleaf属性有什么区别:th:include和th:replace?
【问题讨论】:
标签: thymeleaf
两个Thymeleaf属性有什么区别:th:include和th:replace?
【问题讨论】:
标签: thymeleaf
根据documentation如果你有这种情况:
<div th:include="..."> content here </div>
片段将被放置在<div> 标记内。
但是当你使用替换时:
<div th:replace="..."> content here </div>
然后<div> 将被替换为内容。
Thymeleaf 可以包含其他页面的部分作为片段(而 JSP 仅包括完整页面)使用 th:include (将包括 将片段的内容放入其主机标签中)或 th:replace(将 实际上用片段的标签替换主机标签)。
【讨论】:
【讨论】:
试着理解这个 假设这是片段
<div th:fragment="targetFragmentToIncludeInOurPage" id="tagWithFragmentAttribute">
<div id="contentGoesHere"></div>
</div>
我们在这些 div 中使用这个
<div id="tagWithReplaceAttribute" th:replace="fragments/header :: targetFragmentToIncludeInOurPage"></div>
<div id="tagWithInsertAttribute" th:insert="fragments/header :: targetFragmentToIncludeInOurPage"></div>
<div id="tagWithIncludeAttribute" th:include="fragments/header :: targetFragmentToIncludeInOurPage"></div>
所以最终输出:
<div id="tagWithFragmentAttribute">
<div id="contentGoesHere"></div>
</div>
<div id="tagWithInsertAttribute">
<div id="tagWithFragmentAttribute">
<div id="contentGoesHere"></div>
</div>
</div>
<div id="tagWithIncludeAttribute">
<div id="contentGoesHere"></div>
</div>
【讨论】:
Thymeleaf 可以使用th:include(将片段的内容包含在其主机标签中)或th:replace(实际上将替换为由片段的主机标签)。这允许将片段分组到一个或多个页面中。
【讨论】: