Java Thymeleaf模板-有没有办法装饰模板而不是包含模板片段?
我第一次使用Thymeleaf,我需要对模板进行澄清。如果我正确理解了文档,我可以在我的页面中包含一个模板——或者只是它的一个片段。举个例子,我可以这样写:Java Thymeleaf模板-有没有办法装饰模板而不是包含模板片段?,java,spring-mvc,thymeleaf,Java,Spring Mvc,Thymeleaf,我第一次使用Thymeleaf,我需要对模板进行澄清。如果我正确理解了文档,我可以在我的页面中包含一个模板——或者只是它的一个片段。举个例子,我可以这样写: 你好,世界 但事实上,我想要的是使用模板的相反方式:不是在页面中包含模板片段,而是在我的模板中包含页面,类似这样: ... ... ??? ... 换句话说,有并没有一种方法可以在Thymeleaf中有一个等价物 谢谢据我所知,你不能。一个可能的解决方案是创建一个总是转发到您的装饰文件的视图解析器,但同时放置模型属性,该属性将具有您
你好,世界
但事实上,我想要的是使用模板的相反方式:不是在页面中包含模板片段,而是在我的模板中包含页面,类似这样:
...
...
???
...
换句话说,有并没有一种方法可以在Thymeleaf中有一个等价物
谢谢据我所知,你不能。一个可能的解决方案是创建一个总是转发到您的
装饰文件的视图解析器
,但同时放置模型
属性,该属性将具有您想要包含的片段的实际路径。好的,正如Sotirios Delimanolis所述,Thymeleaf不支持使用模板的那种方式,或者我应该说“分层布局”,正如Daniel Fernandez在中解释的那样
由于sitemesh和Thymeleaf是兼容的,因此我似乎必须同时使用这两种解决方案。太糟糕了
编辑:正如DennisJaamann在评论中建议的那样,我最终使用了一种视图方言,它提供了我所寻找的功能
工作守则:
首先,我添加了layoutdialine
类:
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
//NB, selecting HTML5 as the template mode.
resolver.setTemplateMode("HTML5");
resolver.setCacheable(false);
return resolver;
}
然后,我创建模板(例如templates/layout.html
),并将layout:fragment
信息添加到当前页面的内容:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
...
</head>
<body>
<div id="my-template-header">...</div>
<div id="the-content" layout:fragment="content">
<!-- include here the content of the current page visited by the user -->
</div>
<div id="my-template-footer">...</div>
</body>
使用Thymeleaf 2.1,您可以编写如下内容:
创建模板(例如templates/layout.html),并在html标记中添加th:fragment=“page”信息,并使用th:include=“this::content”信息定义内容区域:
试验
版面页
版面页脚
现在创建包含此模板的页面,在html标记中添加th:include=“templates/layout::page”,并将主要内容放在带有th:fragment=“content”
我的页面内容
在布局页面中,您可以使用this
(th:include=“this::content”)或抑制此选项(th:include=“::content”)。我想这看起来像是jsf facelets。您需要
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>1.2.2</version>
</dependency>
如果现在在视图文件夹中创建名为template的文件夹
views/home.html
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="template/layout">
<body>
<div layout:fragment="content">
Hello world
</div>
</body>
</html>
你好,世界
视图/layout/layout.html
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
</head>
<body>
<div id="content" layout:fragment="content">
</div>
</body>
</html>
你试过了吗?@DennisJaamann我现在确实在测试它。形式上,你应该添加解决它的代码,并接受你自己的答案:)@DennisJaamann你是对的,我想添加代码,即使它与Layout project文档中提供的代码完全相同。为了接受我的回答,我不得不等到明天:)非常有帮助。谢谢。您好,您知道有没有一种方法可以使用布局方言来保存父级内容,而不是覆盖它?意思:最终结果将呈现Hello-world
,而不仅仅是Hello-world
。当然,这不需要移动
到
之外。谢谢你,这很适合我们这些曾经tag@VadimFerderer:我不明白你的意见。这工作得很好-您可以在中创建布局页面,并且每个页面都包含标题标记,您将在其中使用该布局页面。@VadimFerderer您的评论没有建设性……标题资源有什么问题?
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="template/layout">
<body>
<div layout:fragment="content">
Hello world
</div>
</body>
</html>
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
</head>
<body>
<div id="content" layout:fragment="content">
</div>
</body>
</html>