我有簡單的SpringBoot應用程序。一切工作正常,但是當我試圖讓的index.html,在那裏我更換片段,如下圖所示,我得到一個異常:更換片段時解決thymeleaf模板時出錯
org.thymeleaf.exceptions.TemplateInputException:錯誤解決模板「〜{佈局」模板可能不存在或可能不被任何配置的模板解析器(指數:2)進行訪問
這裏是我的代碼:
的build.gradle通過SPRING INITIALIZR產生:
buildscript {
ext {
springBootVersion = '1.5.7.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.bearcave'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-web")
runtime('org.postgresql:postgresql')
compileOnly('org.projectlombok:lombok')
}
主要:
@SpringBootApplication
public class MessengerApplication {
public static void main(String[] args) {
SpringApplication.run(MessengerApplication.class, args);
}
}
控制器:
@Controller
public class HelloWorldController {
@GetMapping("/")
public String home() {
return "index";
}
}
指數:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:replace="~{layout :: layout (~{::body})}">
<body>
<div><p>Hello world!</p></div>
</body>
</html>
佈局:
<!doctype html>
<html th:fragment="layout (template)" xmlns:th="http://www.w3.org/1999/xhtml" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<title>Messenger</title>
</head>
<body>
<div th:replace="${template}"></div>
</body>
</html>
據我所知,Spring Boot自動配置,.html頁面的默認路徑是資源/模板,所以我有。此外,當我不嘗試替換片段時(我收到帶有「Hello world!」文本的簡單頁面)沒有問題。
我能做些什麼才能使它正常工作?