Thymeleaf包含和替换之间的区别?

cod*_*ash 20 thymeleaf

两个Thymeleaf属性之间有什么区别:th:includeth:replace

Iwo*_*ski 24

根据文档,如果你有这种情况:

<div th:include="..."> content here </div>
Run Code Online (Sandbox Code Playgroud)

片段将放在<div>标签内.

但是当你使用替换时:

<div th:replace="..."> content here </div>
Run Code Online (Sandbox Code Playgroud)

然后<div>将被内容取代.

Thymeleaf可以将其他页面的一部分作为片段(而JSP仅包括完整页面)使用th:include(将片段的内容包含到其宿主标签中)或th:replace(实际上将用片段替换宿主标签).

  • 从3.0开始,不建议使用include(参见http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#difference-between-thinsert-and-threplace-and-thinclude) (4认同)

Abd*_*hçı 6

摘自拜尔东

\n\n

包含片段内容的基本方法有以下三种:

\n\n
    \n
  • insert \xe2\x80\x93 在标签内插入内容
  • \n
  • Replace \xe2\x80\x93 用定义片段的标签替换当前标签
  • \n
  • include \xe2\x80\x93 已弃用,但它仍可能出现在旧代码中
  • \n
\n


Sac*_*mar 6

尝试用这个来理解假设这是片段

<div th:fragment="targetFragmentToIncludeInOurPage" id="tagWithFragmentAttribute">
 <div id="contentGoesHere"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

我们在这些 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>
Run Code Online (Sandbox Code Playgroud)

所以最终输出:

<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>
Run Code Online (Sandbox Code Playgroud)