ben*_*bia 3 java spring-mvc thymeleaf spring-boot
好的,我正在使用百里香页面布局.
它似乎工作正常,但是,我想知道如何<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>
Run Code Online (Sandbox Code Playgroud)
这是我的索引页面(我希望显示标题标签):
<!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>
Run Code Online (Sandbox Code Playgroud)
这是实际的HTML输出:
<head>
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
Run Code Online (Sandbox Code Playgroud)
我知道发生了什么.头文件片段被加载(包含)到我的索引页面的头部,因此它覆盖了所有属性.
我的问题是我不知道如何有一个标题片段并且在指定属性时让文件使用该片段,如标题.
这是我在索引文件中尝试过的:
<head>
<div th:include="fragments/header :: header"></div>
<title>THIS DOES NOT SHOW</title>
</head>
Run Code Online (Sandbox Code Playgroud)
但它真的搞砸了我的HTML.有一个简单的解决方案吗?
好的,我找到了一种使用此解决方案的方法.我仍然认为它很笨重,我仍然认为必须有更好的方法,但是对于我的需求来说这已经足够了(如果有的话,请分享一个解决方案):
神奇的是th:remove="tag",在运行时,删除了对象标记并适当地显示了片段:
<head>
<title>THIS DOES SHOW</title>
<object th:include="fragments/header :: header" th:remove="tag"><object>
</head>
Run Code Online (Sandbox Code Playgroud)
所以现在输出是:
<head>
<title>THIS DOES SHOW</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2206 次 |
| 最近记录: |