首页 > 解决方案 > 布局:装饰不能在百里香叶中解决

问题描述

我已将以下 html 标记添加到引用的文件中base.html

<html 
  xmlns:th="http://www.thymeleaf.org" 
  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
  layout:decorate="~{base}"
>

但是,每当我调用它时,都会失败并出现以下错误:

[THYMELEAF][http-nio-8080-exec-5] Exception processing template "register": 
An error happened during template parsing (template: "/Users/david/IdeaProjects/Greeting/src/main/resources/templates/register.html")

当我删除该行时,html 呈现正常,但显然没有导入 base.html 模板文件。这是我的项目结构:

- resources
  - templates
    - base.html
    - register.html

最后,我在我的 WebConfig 中添加了这个:

private TemplateEngine templateEngine(ITemplateResolver templateResolver) {
    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.setTemplateResolver(templateResolver);
    engine.addDialect(new LayoutDialect());
    return engine;
}

标签: javaspringthymeleaf

解决方案


这样做:

layout:decorator="base"

请注意,该标签称为“装饰器”而不是“装饰”。

这里有详细的说明


推荐阅读