我正在关注本教程:http ://www.thymeleaf.org/doc/layouts.html (进入 Thymeleaf 布局方言部分)。在那里你可以找到一个例子:
<!DOCTYPE html>
<html>
<head>
<!--/* Each token will be replaced by their respective titles in the resulting page. */-->
<title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE">Task List</title>
...
</head>
<body>
<!--/* Standard layout can be mixed with Layout Dialect */-->
<div th:replace="fragments/header :: header">
...
</div>
<div class="container">
<div layout:fragment="content">
...
</div>
<div th:replace="fragments/footer :: footer">© 2014 The Static Templates</div>
</div>
</body>
</html>
th:replace
在上面的例子中,页脚和页眉被标签替换,而布局文件中<head>
有<title>
标签。
基本上,我想<head>
用th:replace
. 因此,我有:
我的布局文件:
<!DOCTYPE html>
<html>
<head th:replace="/html/components/head :: head">
</head>
<body>
<div layout:fragment="content">
</div>
...
<div th:replace="/html/components/footer :: footer" />
</body>
<html>
我的内容文件:
<!DOCTYPE html>
<html layout:decorator="/html/layouts/layout">
<head>
<title>My content title</title>
</head>
<body>
<div layout:fragment="content">
...
</div>
</body>
</html>
最后是我的 /html/components/head.htm 文件:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head th:fragment="head">
<meta charset="utf-8" />
<title layout:title-pattern="$CONTENT_TITLE">Layout Title should be replaced by Content Title!</title>
...
</head>
<body>
</body>
</html>
内容还行。按预期从文件中包含(替换)页脚和标题,但页面标题为空白!
我得到:
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title></title>
...
怎么了?