Thymeleaf装饰器无法正常工作

时间:2015-02-07 17:20:08

标签: spring-boot decorator thymeleaf

我创建了一个新的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类,我在其中添加了dialact。并解决视图。

@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 app运行时,我只看到内容“list ...” htmls的路径是正确的。 你能告诉我我做错了什么吗?我也认识到,当我使用webjar中的bootstrap样式表时,没有加载。

非常感谢你。

1 个答案:

答案 0 :(得分:2)

在这里看到两个错误。首先你需要和百里香叶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>