2015-02-07 73 views
3

我創建了一個新的Spring引導工程,並希望使用Thymeleaf和LayoutDialect。 我的pom.xml有以下依賴性:Thymeleaf裝飾工程師無法正常工作

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

當我運行它作爲春季啓動應用程序我只看到內容「列表...」 通往htmls ar的路徑e正確。 你能告訴我我做錯了什麼嗎?我也認識到,當我使用webjar的引導樣式表時沒有加載。

非常感謝。

回答

2

在這裏看到兩個錯誤。首先你需要和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> 
+0

我有同樣的問題。這個答案對我有用。我的情況是,缺少xmlns:layout命名空間是特定的問題。 – 2017-07-13 23:23:20