Spring Boot和Thymeleaf:找不到HTML模板
我试图用Thymeleaf创建一个基于Spring Boot" title="Spring Boot">Spring Boot的应用程序。我以PetClinic示例为起点。我的应用程序找不到某些模板。
我的项目以标准方式设置:
/src /main
/java
/com.example
MyWebApplication.java
HomeController.java
/resources
/static
/css
/templates
/fragments
layout.html
home.html
application.properties
<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>
@SpringBootApplicationpublic class MyWebApplication {
public static void main(String[] args) {
SpringApplication.run(MyWebApplication.class, args);
}
}
@Controllerpublic class HomeController {
@RequestMapping("/")
public String home() {
return "home";
}
}
<!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>
<!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>
为空。
当我导航到http://localhost:8080
我得到错误:
解决模板“〜{fragments / layout”时出错,该模板可能不存在,或者任何已配置的模板解析器都无法访问(home:5)
如果我从home.html删除’th:replace’属性,它将显示欢迎消息。
浏览类似的问题(例如,这个问题),我看到其他人创建了ThymeleafViewResolver
bean。
问题:
- 为什么找不到我的模板?
- 我需要创建模板解析器吗?如果是这样,宠物诊所样本为什么不呢?它的模板解析器来自哪里?
据我所知,它的设置方式与宠物诊所的样本相同,所以有什么区别?
回答:
Spring
Boot会自动为您完成一些内部工作,但是要进行自动工作,您需要对其进行适当的配置。这是您问题2的答案,如果配置正确,spring会自动提供视图解析器。好的,现在转到问题1。
如果将pom.xml与petclinic的pom.xml进行比较,您将看到一些关键差异。
首先,根据spring官方文档http://docs.spring.io/spring-
boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-
maven,Maven用户可以继承从spring-boot-starter-parent项目中获取合理的默认值。父项目提供了一些功能。要将项目配置为从spring-
boot-starter-parent继承,只需在pom.xml中设置父项即可。
<parent> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
使用该设置,您还可以通过覆盖自己项目中的属性来覆盖各个依赖项。例如,在petclinic pom.xml中,他们指定了您的百里香叶版本。
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
现在,要正确设置Thymeleaf和Thymeleaf布局方言版本并使其兼容,您需要从spring-boot-starter-
thymeleaf中排除thymeleaf-layout-dialect,即将thymeleaf版本设置为3.0.2.RELEASE版本。
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<exclusions>
<exclusion>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</exclusion>
</exclusions>
</dependency>
以上是 Spring Boot和Thymeleaf:找不到HTML模板 的全部内容, 来源链接: utcz.com/qa/415936.html