thymeleaf里insert、replace、include的区别

thymeleaf里insert、replace、include之间有些什么区别?

其实这3者都很相像。都是引入片段。区分起来的话:

th:insert 插入片段,保留自身标记

th:replace 插入片段,替换了自身标记

th:include 插入片段的内容,去掉片段外层标记,同时保留自身标记

比如有这么个标记

<footer th:fragment="copy">
  &copy; 2011 The Good Thymes Virtual Grocery
</footer>

然后分别用insert、replace、include来引用:

<body>

  <div th:insert="footer :: copy"></div>

  <div th:replace="footer :: copy"></div>

  <div th:include="footer :: copy"></div>
  
</body>

运行结果:

<body>

  <div>
    <footer>
      &copy; 2011 The Good Thymes Virtual Grocery
    </footer>
  </div>

  <footer>
    &copy; 2011 The Good Thymes Virtual Grocery
  </footer>

  <div>
    &copy; 2011 The Good Thymes Virtual Grocery
  </div>
  
</body>
发布了1105 篇原创文章 · 获赞 337 · 访问量 338万+

猜你喜欢

转载自blog.csdn.net/leftfist/article/details/103613578
今日推荐