Java Thymeleaf 模板 - 有没有办法装饰模板而不是包含模板片段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18896915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thymeleaf templates - Is there a way to decorate a template instead of including a template fragment?
提问by Romain Linsolas
I am working with Thymeleaf for the first time, and I need a clarification about the templates. If I correctly understand the documentation, I can includea template - or just a fragment of it - in my page. So for example, I can write something like that:
我是第一次使用 Thymeleaf,我需要对模板进行说明。如果我正确理解了文档,我可以在我的页面中包含一个模板——或者只是它的一个片段。例如,我可以这样写:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head th:include="template/layout :: header">
</head>
<body>
Hello world
<div th:include="template/layout :: footer"></div>
</body>
</html>
But what I want is in fact the opposite way of using the template : instead of including template fragment in the page, I want to include the page insidemy template, something like that:
但实际上我想要的是使用模板的相反方式:我想将页面包含在我的模板中,而不是在页面中包含模板片段,类似这样:
<!DOCTYPE html>
<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">
<!-- include here the content of the current page visited by the user -->
???
</div>
<div id="my-template-footer">...</div>
</body>
In others words, is there a way to have an equivalent of the Sitemesh decorators tagsin Thymeleaf?
换句话说,有没有办法在 Thymeleaf 中使用等效的Sitemesh 装饰器标签?
Thanks
谢谢
采纳答案by Romain Linsolas
Ok, as stated by Sotirios Delimanolis, Thymeleaf does not support that way of using template, or should I say "Hierarchical layouts", as explained by Daniel Fernandez in this thread.
好的,正如 Sotirios Delimanolis 所说,Thymeleaf 不支持这种使用模板的方式,或者我应该说“分层布局”,正如 Daniel Fernandez 在此线程中所解释的那样。
As sitemesh and Thymeleaf are compatible, it seems that I have to use both solutions. Too bad.
由于 sitemesh 和 Thymeleaf 是兼容的,看来我必须同时使用这两种解决方案。太糟糕了。
Edit: As suggested by DennisJaamann in a comment, I finally used Thymeleaf Layout Dialect, a view dialect that provides the feature I was looking for.
编辑:正如 DennisJaamann 在评论中所建议的,我最终使用了Thymeleaf Layout Dialect,这是一种提供我正在寻找的功能的视图方言。
The working code:
工作代码:
First I add the LayoutDialect
class:
首先我添加LayoutDialect
类:
@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;
}
Then, I create the template (for ex. templates/layout.html
), and add the layout:fragment
information where I want to put the content of the current page:
然后,我创建模板(例如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>
and the page will refers to the template with the attribute layout:decorator
:
并且页面将引用具有属性的模板layout:decorator
:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="templates/layout">
<body>
<div layout:fragment="content">
Hello world
</div>
</body>
</html>
回答by Sotirios Delimanolis
As far as I know, you can't. A possible solution would be to create a ViewResolver
that always forwards to your decorator
file, but at the same time put Model
attributes that would have the actual path to the fragment you want to include.
据我所知,你不能。一个可能的解决方案是创建一个ViewResolver
始终转发到您的decorator
文件的Model
属性,但同时放置具有您想要包含的片段的实际路径的属性。
回答by éricoGR
with Thymeleaf 2.1, you can write something like that:
使用 Thymeleaf 2.1,你可以这样写:
Create the template (for ex. templates/layout.html), and add the th:fragment="page"information in html tag and define the content area with th:include="this :: content"information:
创建模板(例如templates/layout.html),在html标签中添加th:fragment="page"信息,并用th:include="this::content"信息定义内容区域:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
th:fragment="page">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test</title>
</head>
<body>
layout page
<div th:include="this :: content"/>
layout footer
</body>
</html>
Now create the page that will include this template adding th:include="templates/layout :: page"in html tag and put your main content inside a div with th:fragment="content"
现在创建将包含此模板的页面,在 html 标签中添加th:include="templates/layout :: page"并将您的主要内容放在一个带有th:fragment="content"的 div 中
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
th:include="templates/layout :: page">
<head>
<title></title>
</head>
<body>
<div th:fragment="content">
my page content
</div>
</body>
</html>
In layout page you can use this(th:include="this :: content") or suppress this option (th:include=":: content"). It seems like jsf facelets I think.
在布局页面中,您可以使用此选项(th:include="this :: content") 或取消此选项 (th:include=":: content")。我认为这似乎是 jsf facelets。
回答by mrclrchtr
You need
你需要
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>1.2.2</version>
</dependency>
and add this to your SpringTemplateEngine:
并将其添加到您的 SpringTemplateEngine 中:
@Bean
@Description("Thymeleaf template engine with Spring integration")
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.addDialect(new LayoutDialect());
return templateEngine;
}
If now create a folder named template in your views folder.
如果现在在您的视图文件夹中创建一个名为模板的文件夹。
views/home.html
视图/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>
views/layout/layout.html
视图/布局/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>