【问题标题】:How can I extend my template with thymeleaf?如何使用百里香扩展我的模板?
【发布时间】:2021-05-25 19:50:38
【问题描述】:

我想要在一个页面中显示这两个文件的视图。我遵循了语法,但我无法获得正确的视图。

布局页面:

<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">

    <body>
        <div layout:fragment="content">
            <p>Changing contents</p>
        </div>
    </body>
</html>

内容页面:

<!DOCTYPE html>
<html lang="en"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="skeleton.html"
      xmlns:th="http://www.thymeleaf.org">

  <body>
    <div layout:fragment="content">

      <!--Content of the page-->
      <h3>page injected in another page</h3>

    </div>
  </body>
</html>

结果只是带有以下文本的内容页面的内容: 页面注入另一个页面

谁能告诉我我的代码有什么问题?

【问题讨论】:

    标签: html templates layout thymeleaf


    【解决方案1】:

    对于layout:decorate,您需要使用Standard Expression Syntax,类似... layout:decorate="~{skeleton}" 将起作用,具体取决于您的skeleton.html 文件的位置。

    【讨论】:

    • 感谢您的帮助。我已经尝试过了(layout:decorate="~{skeleton}"),但它不起作用,而我的骨架文件直接在模板目录中(不在子目录中)。而且我仍然得到“页面注入另一页”文本,仅此而已。布局页面不显示。我还能尝试什么?
    • 根据manual配置布局方言了吗?此外,在描述中的每个 HTML sn-p 上方添加文件名。我很想知道你的skeleton.html 在哪里。提供您用于显示结果页面的 URL。
    • 我不知道应该在哪里复制/粘贴“模板引擎”的代码。我没有看到具有此名称的文件。我应该在哪里添加此代码?
    • 在您的 Thymeleaf 配置中。这个article 对你有用吗?通读一遍,然后做同样的事情。
    • 我只有一个名为 ThymeleafAutoConfiguration.class 的类,我应该在这个类中添加代码吗?因为我在 baeldung 网站上看到了“ThymeleafConfiguration”。
    猜你喜欢
    • 2015-11-29
    • 2015-06-01
    • 2019-11-13
    • 1970-01-01
    • 2021-01-09
    • 2019-05-20
    • 2017-11-03
    • 2019-12-19
    • 2013-02-03
    相关资源
    最近更新 更多