java – Thymeleaf页面布局标题属性覆盖
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Thymeleaf页面布局标题属性覆盖,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3533字,纯文字阅读大概需要6分钟。
内容图文
![java – Thymeleaf页面布局标题属性覆盖](/upload/InfoBanner/zyjiaocheng/727/816cc064fc4c4b8287bd71614ae62ee2.jpg)
好的,我正在使用thymeleaf page layouts.
它似乎工作正常,但是,我想知道如何配置< title>< / title> < head>< / head>中的属性一个使用片段的页面?目前,头部中的所有内容都被标题片段覆盖(可以在下面看到).
这是我的:resources / templates / fragments / header.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head th:fragment="header">
<link href="../../static/css/bootstrap.min.css" th:href="@{css/bootstrap.min.css}" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js" th:src="@{js/bootstrap.min.js}"></script>
</head>
<body></body>
</html>
这是我的索引页面(我希望显示标题标签):
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head th:include="fragments/header :: header">
<title>THIS DOES NOT SHOW</title>
</head>
<body>
<div class="container">
<div th:include="fragments/headerNavbar :: headerNavbar"></div>
<h1>hey</h1>
<div class="btn btn-success">button</div>
<button class="btn btn-success">another</button>
</div>
这是实际的HTML输出:
<head>
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
我知道发生了什么.头文件片段被加载(包含)到我的索引页面的头部,因此它覆盖了所有属性.
我的问题是我不知道如何有一个标题片段并且在指定属性时让文件使用该片段,如标题.
这是我在索引文件中尝试过的:
<head>
<div th:include="fragments/header :: header"></div>
<title>THIS DOES NOT SHOW</title>
</head>
但它真的搞砸了我的HTML.有一个简单的解决方案吗?
解决方法:
您可以使用Thymeleaf Layout Dialect,它通过新属性为分层布局提供支持.这些属性类似于Thymeleaf属性,但提供了额外的功能.
例如,它允许将布局头部分的内容与提供主体部分的页面中的头部分的内容混合.
您必须创建一个模板以用作页面的布局.根据你的例子,你会有这样的事情:
文件资源/模板/布局/ layout.html
<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<link href="../../static/css/bootstrap.min.css" th:href="@{css/bootstrap.min.css}" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js" th:src="@{js/bootstrap.min.js}"></script>
<title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE">My app</title>
</head>
<body>
<section layout:fragment="content">
<!-- Content replaced by each page's content fragment -->
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Praesent scelerisque neque neque, ac elementum quam dignissim interdum.
</p>
</section>
</body>
</html>
文件资源/ templates / index.html
<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="layouts/layout">
<head>
<title>Home page</title>
</head>
<body>
<div layout:fragment="content">
<h1>hey</h1>
<div class="btn btn-success">button</div>
<button class="btn btn-success">another</button>
</div>
</body>
</html>
这样生成的页面将具有以下头部分:
<head>
<link href="../../static/css/bootstrap.min.css" th:href="@{css/bootstrap.min.css}" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js" th:src="@{js/bootstrap.min.js}"></script>
<title>My app - Home page</title>
</head>
请注意layout:title-pattern属性,它允许设置模式以生成最终标题.默认情况下,布局方言将合并页面头部中包含的任何脚本或样式表.为了简单起见,我没有为页眉,页脚,菜单等包含任何片段,但也支持它.
查看项目页面,了解您必须添加到项目中的依赖项以及所需的配置.如果您正在使用启动,Thymeleaf启动器已经支持此方言,因此您只需将依赖项添加到项目中,它将自动配置.
编辑:我已经更正了布局示例中的拼写错误,使用的是布局:include而不是layout:fragment.
内容总结
以上是互联网集市为您收集整理的java – Thymeleaf页面布局标题属性覆盖全部内容,希望文章能够帮你解决java – Thymeleaf页面布局标题属性覆盖所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。