Thymeleaf 中的标题和标题

2024-03-01

我是百里香初学者。 我从一个通用的布局页面开始:

片段/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>

和内容页面:

页面.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 标签中),但有一个per-page title?


我也遇到这个问题了(谢谢nmy https://stackoverflow.com/users/4165181/nmy供参考文档 http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#including-template-fragments!)这是我注意到的以及我如何在我的应用程序中解决它:

文档中需要注意的事项:

  1. 之间的差异th:include and th: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>

希望这可以帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Thymeleaf 中的标题和标题 的相关文章

  • 在 Spring Security 中使用 CSRF 令牌获取 403

    我遇到了多个选项卡的问题 如果我从第一个选项卡注销并打开另一个选项卡 登录并注销后 如果我返回第一个选项卡并登录 我会收到 403 例如 第一个选项卡的注销页面已由 spring security 和 thymeleaf 添加到表单中
  • 如何在Thymeleaf和Spring Boot中显示消息?

    我创建了一个使用 Thymeleaf 作为模板引擎的 Spring Boot Web 应用程序 我配置了MessageSource在子文件夹中查找邮件 Bean public MessageSource messageSource fina
  • 使用 thymeleaf 中的搜索功能和请求参数

    我有一个页面 可以在其中获取条目列表 现在 我希望能够从这些列表中进行搜索 我当前用于检索列表的网址是 show products 我想在此页面中添加一个搜索表单 以便我可以使用请求参数进行搜索 是的 我可以使用ajax 但我必须使用请求参
  • 使用 HashMap 作为表单支持 Bean Spring MVC + ThymeLeaf

    我是 Spring MVC 的新手 来自 Grails 是否可以使用 HashMap 作为表单支持 bean 在 Grails 中 人们可以从任何控制器操作访问名为 params 的对象 Params 只是一个映射 其中包含 POST 数据
  • 如何使用 spring mvc 访问 thymeleaf 模板中的变量

    如何在 thymeleaf 模板内访问在 Spring MVC 中添加到模型中的变量 我有以下控制器代码 Controller public class ThymeLeafController GetMapping thyme public
  • 如何在填充表的循环内的 thymeleaf 中执行 if else

    我正在使用 a 填充表格 tr 我想放置一个 if 语句来评估该值是否为空 如果该值为空 我想放置此 而不是 null 我怎样才能使用th if或其他类似的功能我是新使用百里香 这是我的代码 table class table table
  • Google Map API 使用 Java/Springboot 和 Thymeleaf 接收坐标但不创建地图

    我正在 JAVA SPRING HTML5 中使用 Thymeleaf 创建一个 Web 应用程序 该应用程序根据 H2 数据库制作带有标记的地图 其中包含所有客户的纬度和经度 我成功进行了后端x前端整合 Java 控制器使用 Thymel
  • Datatable:日期/时间排序插件未排序

    我有一个基本的 Spring Boot 应用程序 嵌入式 Tomcat Thymeleaf 模板引擎 我想订购数据表的 1 个日期列 在我的 POJO 中 public String getTimeFormatted DateTimeFor
  • thymeleaf 内联 javascript 框架问题

    打印到文件中的替换文本与 Jackson 库的 ObjectMapper 所做的不同 对于上面示例中的 Thymeleaf listObject 将是 dataType type DataType name STRING friendlyN
  • Spring Boot Thymeleaf 布局方言不起作用

    我刚刚创建了一个新的 Spring Boot v1 5 项目 并面临 Thymeleaf 布局方言无法工作的问题 我的 build gradle 及其类路径中有依赖项 compile group nz net ultraq thymelea
  • 使用 thymeleaf 请求参数

    在 Spring Boot Web 应用程序中 User想要重置密码 所以他输入Reset password页 现在我想让他输入他的电子邮件地址 pushReset我想重定向到myapp resetPassword email HIS EM
  • 同时使用 Thymeleaf 和 JSP

    我使用的是 JSP JSTL 但我对 c if c choose 感到厌烦 因此 我希望我的 JSP 页面同时使用 JSP 和 Thymeleaf 来呈现 我计划尽快删除所有 JSTL 我正在使用春季MVC框架
  • Spring 类级别验证和 Thymeleaf

    我正在学习 Spring 框架和 Thymeleaf 我已经知道如何使用类似的东西来显示字段错误 fields errors xx 但是 我对如何在 Thymeleaf 中显示对象错误消息感到困惑 这是我的UserForm class Pa
  • Thymeleaf 不解释 sec 标签

    我遇到了一个问题 thymleaf 无法识别我的 Spring Boot 项目中的 sec 标签 例如下面的 sec authentication 未被解释 并按浏览器中的 html 中的形式显示 div Roles span span d
  • Intellij+Springboot+Thymeleaf+gradle - 自动重新加载html资源

    我正在使用 IntelliJ Ultimate Spring Boot 和 Thymeleaf 我想启用 HTML 自动重新加载 而无需重新启动服务器 也无需使用 CTRL F9 我已经阅读了以下内容 我认为它应该有效 但事实并非如此 ht
  • Thymeleaf 中链接绝对 URL 时 th:href 和 href 之间的区别

    就在 Thymeleaf 的开头文档 http www thymeleaf org doc articles standardurlsyntax html关于标准url语法有两个例子 但没有说明它们之间的区别 a a href http w
  • 使用 Thymeleaf 和 Spring Boot 转换器列出 JSON 字符串

    我正在开发一项通过 Thymeleaf 模板生成 HTML 页面的服务 在其中一个模板中 我希望有一个 HTML 属性作为 JSON 字符串 我的上下文中的相关对象是ArrayList
  • Thymeleaf 文本文字中的下划线

    问题 如何转义文本中的多个连续下划线 我正在使用 HTML 的标准 Thymeleaf 方言 我是not此处使用 Spring 或 SpEL 在 Thymeleaf 中 我可以创建一个下划线作为文字文字 https www thymelea
  • 如何将列表从控制器发送到百里香

    我需要将列表中存在的数据从我的 spring 控制器发送到 thymeleaf html 我如何发送数据 收到后如何迭代百里香中的数据 将您的清单放入您的ModelMap or ModelAndView对象然后使用遍历它th each在你的
  • Thymeleaf:传递 javascript 参数

    我有一个基本的 SpringBoot 应用程序 使用 Spring Initializer 嵌入式 Tomcat Thymeleaf 模板引擎 并打包为可执行 JAR 文件 我想将 POJO 的属性传递给 javascript 函数 tr

随机推荐