ThymeleafでSpringブートベースのアプリケーションを作成しようとしています。私は開始点としてPetClinicサンプルを使用しています。 私のアプリケーションは、いくつかのテンプレートを見つけることができません。Springブートと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);
}
}
が空0 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。私は「目:交換する」削除した場合
Error resolving template "~{fragments/layout", template might not exist or might not be accessible by any of the configured Template Resolvers (home:5)
:私はhttp://localhost:8080
に移動すると
が、私はエラーを取得するhome.htmlから属性を、それがウェルカムメッセージが表示されます。
類似の質問(例:this one)を参照すると、他の人がThymeleafViewResolver
beanを作成することがわかります。
質問:
- は、なぜそれが私のテンプレートを見つけることができませんか?
- テンプレートリゾルバを作成する必要はありますか?もしそうなら、どのようにペットクリニックのサンプルはありませんか?テンプレートリゾルバはどこから来たのですか?
私が見る限り、それはペットクリニックのサンプルと同じ方法で設定されているので、違いは何ですか?
thymeleaf-layout-dialectアーチファクトを除外する必要があるのはなぜですか? – devdanke
トレーニングホイールの "-starter"アーティファクトなしでthymeleafとspring-bootを使用する方法を示す例はありますか? "-starter"アーティファクトは、デモ/ラーニングの目的であるようです。残念ながら、彼らはthymeleafとspring-bootが実際にどのように動作するかを隠しています。 – devdanke