thymeleaf - Thymeleaf 中的标题和标题

标签 thymeleaf

我是 Thymeleaf 初学者。 我从一个通用布局页面开始:

fragments/layout.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:fragment="headerFragment">
    <title>Template title</title>
    <!-- metas, link and scripts -->
</head>
<body>
<div class="container">
    Some text
</div>
</body>
</html>

和内容页面:

page.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="fragments/layout :: headerFragment">
    <title>Page title</title>
    <!-- metas, link and scripts -->
</head>
<body>
<div class="container">
    Some text
</div>
</body>
</html>

当我渲染页面时,标题始终来自模板,而不是页面。 Thymeleaf 中是否可以有共同的元数据、脚本和样式(在 HEAD 标签中),但有一个每页标题?

最佳答案

我也遇到了这个问题(感谢nmy引用documentation!)这是我注意到的以及我如何在我的应用程序中解决它:

文档中需要注意的事项:

  1. th:includeth:replace 之间的区别
  2. 通过 domselector 而不是通过 th:fragment 引用片段
  3. Thymeleaf 提供了“this”选项来查找选择器

考虑到这三件事,您可以执行以下操作:

片段/layout.html:

<head th:fragment="headerFragment">
    <title th:include=":: #pageTitle">Layout Generic Title< /title>
    <!-- metas, link and scripts -->
</head>

页面.html

<head th:include="fragments/layout :: headerFragment">
    <title id="pageTitle">Page title</title>
    <!-- other elements you want to reference in your layout -->
</head>

希望这有帮助!

关于thymeleaf - Thymeleaf 中的标题和标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31451830/

相关文章:

spring-boot - CSS 中的服务器上下文路径和图像 url

java - 无法使用 Spring MVC 在 HTML ArrayList 中打印

javascript - 从 thymeleaf 获取对象到 js

java - 是否有 Roo 应用程序与外部 REST 服务交互以进行查找和/或验证的示例

javascript - Spring MVC 和 Thymeleaf 资源版本控制

html - 将 css 类添加到输入标签 thymeleaf + spring

java - Flying Saucer/OpenPdf相同的字符串html内容但不同的字节[]

java - HTML 中的第一个提交按钮重定向到不同的 URL

javascript - 当列数据溢出时,如何使 jquery 数据表中的单列可滚动?

javascript - 在 Thymeleaf 中 & 被解析为 &