Thymeleaf模板-有没有办法装饰模板而不是包含模板片段?

Thymeleaf模板-有没有办法装饰模板而不是包含模板片段?,第1张

Thymeleaf模板-有没有办法装饰模板而不是包含模板片段?

好的,如Sotirios Delimanolis所述,Thymeleaf不支持这种使用模板的方式,或者我应该说“ 分层布局 ”,正如Daniel
Fernandez在此主题中所解释的那样。

由于sitemesh和Thymeleaf是兼容的,因此似乎必须同时使用这两种解决方案。太糟糕了。

编辑:正如DennisJaamann在评论中建议的那样,我最终使用了Thymeleaf Layout
Dialect
,这是一种提供我所需要的功能的视图方言。

工作代码:

首先,我添加

LayoutDialect
类:

@Beanpublic 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>

并且页面将引用具有属性的模板

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>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5641634.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存