Spring Boot Thymeleaf装饰器不工作

nbnkbykc  于 2023-06-29  发布在  Spring
关注(0)|答案(2)|浏览(158)

我创建了一个新的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上调用decorator…

<!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>

当我作为Sping Boot 应用程序运行它时,我只看到内容“列表...”HTMLS的路径是正确的。你能告诉我我哪里做错了吗?我还意识到,当我使用webjar中的bootstrap样式表时,它们没有加载。
非常感谢。

vfwfrxfs

vfwfrxfs1#

这里有两个错误。首先,你需要在thymeleaf xml中有空间,就像这样。

<!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>
aij0ehis

aij0ehis2#

我已经花了很多时间与问题,它不起作用。网上很多例子都说:

layout:decorator=“layout”

但事实是在现实中

layout:decorate=“layout”

相关问题