【问题标题】:Difference between Thymeleaf include and replace?Thymeleaf 包含和替换之间的区别?
【发布时间】:2016-09-03 09:23:14
【问题描述】:

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

【问题讨论】:

    标签: thymeleaf


    【解决方案1】:

    根据documentation如果你有这种情况:

    <div th:include="..."> content here </div>
    

    片段将被放置在&lt;div&gt; 标记内。

    但是当你使用替换时:

    <div th:replace="..."> content here </div>
    

    然后&lt;div&gt; 将被替换为内容。

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

    【讨论】:

    【解决方案2】:

    取自baeldung

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

    • insert – 在标签内插入内容
    • replace – 用定义片段的标签替换当前标签
    • 包括 - 这已被弃用,但仍可能出现在旧代码中

    【讨论】:

      【解决方案3】:

      试着理解这个 假设这是片段

      <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>
      

      【讨论】:

        【解决方案4】:

        Thymeleaf 可以使用th:include(将片段的内容包含在其主机标签中)或th:replace(实际上将替换为由片段的主机标签)。这允许将片段分组到一个或多个页面中。

        【讨论】:

          猜你喜欢
          • 2014-06-15
          • 1970-01-01
          • 2016-06-14
          • 1970-01-01
          • 1970-01-01
          • 2012-10-26
          • 1970-01-01
          • 2022-10-18
          • 1970-01-01
          相关资源
          最近更新 更多