我正在嘗試用Thymeleaf創建一個基於Spring Boot的應用程序。我使用PetClinic樣本作爲起點。 我的應用程序找不到一些模板。Spring Boot and Thymeleaf:找不到HTML模板
我的項目是建立在標準的方式:
/src
/main
/java
/com.example
MyWebApplication.java
HomeController.java
/resources
/static
/css
/templates
/fragments
layout.html
home.html
application.properties
的pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
MyWebApplication.java
@SpringBootApplication
public class MyWebApplication {
public static void main(String[] args) {
SpringApplication.run(MyWebApplication.class, args);
}
}
HomeController.java
home.html做爲
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
th:replace="~{fragments/layout :: layout (~{::body},'home')}">
<body>
<h2 th:text="#{welcome}">Welcome</h2>
<div class="row">
</div>
</body>
</html>
的layout.html
<!DOCTYPE html>
<!--
The main layout fragment that defines the overall layout
Sets up the navigation bar
The page contents are replaced in the main div
-->
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
th:fragment="layout (template, menu)">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- <link rel="shortcut icon" type="image/x-icon" th:href="@{/images/favicon.png}"> -->
<title>Web Interface</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" />
</head>
<body>
<!-- Navigation Bar -->
<nav class="navbar navbar-default" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand glyphicon glyphicon-home" th:href="@{/}"></a>
<!-- Collapse the menu bar on small windows -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#main-navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- The Navbar items -->
<div class="navbar-collapse collapse" id="main-navbar">
<ul class="nav navbar-nav navbar-right">
<!-- Define a fragment for each menu item -->
<li th:fragment="menuItem (path, active, title, glyph, text)" th:class="${active==menu ? 'active' : ''}">
<a th:href="@{__${path}__}" th:title=${title}>
<span th:class="'glyphicon glyphicon-'+${glyph}" class="glyphicon glyphicon-home" aria-hidden="true"></span>
<span th:text="${text}">Template</span>
</a>
</li>
<!-- Replace with the fragment -->
<li th:replace="::menuItem ('/', 'home', 'home page', 'home', 'Home')">
<span class="glyphicon glyphicon-home" aria-hidden="true"></span>
<span> Home</span>
</li>
</ul>
</div>
</div>
</nav>
<!-- Main body -->
<div class="container-fluid">
<div class="container xd-container">
<div th:replace="${template}"></div>
</div>
</div>
<script th:src="@{/js/bootstrap.min.js}" type="text/javascript"></script>
</body>
</html>
application.properties是空的。
當我瀏覽到http://localhost:8080
我得到的錯誤:
Error resolving template "~{fragments/layout", template might not exist or might not be accessible by any of the configured Template Resolvers (home:5)
如果我刪除了「日:更換」從home.html的屬性,它會顯示歡迎信息。
瀏覽類似問題(例如this one),我看到其他人創建了ThymeleafViewResolver
bean。
問題:
- 爲什麼不能找到我的模板?
- 我是否需要創建模板解析器?如果是這樣,那麼寵物診所樣本怎麼沒有?它的模板解析器來自哪裏?
據我所見,它的設置方式與寵物診所樣品一樣,那麼有什麼區別?
爲什麼需要排除百里香佈局方言神器? – devdanke
是否有任何示例顯示如何使用沒有訓練輪「啓動器」工件的thymeleaf和spring-boot? 「起始器」工件似乎是用於演示/學習的目的。不幸的是,它們隱藏了胸肉和彈簧引導的實際工作方式。 – devdanke