【问题标题】:Thymeleaf decorator is not workingThymeleaf 装饰器不起作用
【发布时间】:2015-02-07 17:20:07
【问题描述】:

我创建了一个新的 Spring-boot 项目,并希望将 Thymeleaf 与 LayoutDialect 一起使用。 我的 pom.xml 具有以下依赖项:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>nz.net.ultraq.thymeleaf</groupId>
        <artifactId>thymeleaf-layout-dialect</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>2.1.3</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>angularjs</artifactId>
        <version>1.3.11</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>angular-ui-router</artifactId>
        <version>0.2.13</version>
    </dependency>
</dependencies>

我还有一个 @Configuration 类,我在其中添加方言。并进行视图解析。

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/projects").setViewName(
            "project/listProjects");
    registry.addViewController("/").setViewName(
            "project/listProjects::content");
    registry.addViewController("/create").setViewName(
            "project/createProject::content");

}


@Bean
public LayoutDialect layoutDialect() {
    return new LayoutDialect();
}

}

我有一个布局 HTML,看起来像

<!DOCTYPE html>
<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>header</div>
         <div  layout:fragment="content">
                <h1>Static content for prototyping purposes only</h1>

        <p>This is the layout of the site. The actual content will come
            from individual views making use of this layout</p>
        </div>
   </body>
</html>

和另一个在布局 html 上调用装饰器的 HTML...

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="layout/layout">
<head></head>
<body>
    <div th:fragment="content">
        <div>list....</div>
    </div>
</body>
</html>

当我将它作为 Spring Boot 应用程序运行时,我只看到内容“列表...” html的路径是正确的。 你能告诉我我做错了什么吗?我也认识到,当我使用 webjar 中的引导样式表时,并没有加载。

非常感谢。

【问题讨论】:

    标签: spring-boot decorator thymeleaf


    【解决方案1】:

    在这里看到两个错误。首先,您需要像这样在 thymeleaf xmlns 中留出空间。

    <!DOCTYPE html>
    <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>header</div>
             <div  layout:fragment="content">
                 <h1>Static content for prototyping purposes only</h1>
                 <p>This is the layout of the site. The actual content will come
                from individual views making use of this layout</p>
            </div>
       </body>
    </html>
    

    第二个有错误

    <div th:fragment="content">
        <div>list....</div>
    </div>
    

    应该是

    <div layout:fragment="content">
        <div>list....</div>
    </div>
    

    【讨论】:

    • 我遇到了同样的问题。这个答案对我有用。我的情况是,缺少 xmlns:layout 命名空间是具体问题。
    猜你喜欢
    • 1970-01-01
    • 2020-04-10
    • 2017-09-28
    • 1970-01-01
    • 2016-03-14
    • 2020-11-09
    • 2015-09-03
    相关资源
    最近更新 更多