简体   繁体   中英

How to replace a fragment dynamically in Thymeleaf?

In the following example, I only want to create a simple main.html layout, and home.html , about.html .

The subpages should only contain their own content body, not header or footer inclusions.

This is what I have so far, but how could I now dynamically replace the container content with the page that was clicked?

<html>
  <body>
      <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
          <div class="navbar-header">
            <a class="navbar-brand" href="/home">Home</a>
          </div>
          <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
              <li class="active"><a href="/about">About</a></li>
            </ul>
          </div>
        </div>
      </div>

    <div class="container">
       <p>This should always get replaced when a link is clicked</p>
       <!--<div th:replace="fragments/pagename :: pagename">-->
    </div>
  </body>
</html>

If I'd only had one page, I could replace the content with <div th:replace="fragments/about :: about"> , but how could I do this dynamically?

The deprecation message came from <html layout:decorate="layout"> . Rewriting it to <html layout:decorate="~{layout}"> fixed the message.

Anyways, thymeleaf seem to lack this important feature, thus using the thymeleaf-layout-dialect layout dialect is the only way to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM