本文介绍了Thymeleaf装饰器无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a new Spring-boot project and wanted to use Thymeleaf with the LayoutDialect. My pom.xml has following dependencies:

<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。并执行视图解析。

I also have a @Configuration class where I add the dialact. And do the view resolving.

@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,看起来像

I have one layout HTML which looks like

<!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上的装饰器...

and an other HTML wich calls decorator on the layout 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的引导样式表时,并没有加载。

when i run it as spring boot app I just see the content "list..."The paths to the htmls are correct.Can you please tell me what I'm doing wrong? I also recognized, that when I used bootstrap stylesheets from the webjar the weren't loaded.

非常感谢。

推荐答案

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

see two errors here. first you need and space in the thymeleaf xmlns like so.

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

这篇关于Thymeleaf装饰器无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 13:17