I am trying to create a Spring Boot-based application with Thymeleaf. I am using the PetClinic sample as a starting point.
My application can't find some of the templates.
@SpringBootApplication
public class MyWebApplication {
public static void main(String[] args) {
SpringApplication.run(MyWebApplication.class, args);
}
}
HomeController.java
@Controller
public class HomeController {
@RequestMapping("/")
public String home() {
return "home";
}
}
<!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 is empty.
When I navigate to http://localhost:8080 I get the error:
Error resolving template "~{fragments/layout", template might not
exist or might not be accessible by any of the configured Template
Resolvers (home:5)
If I remove the 'th:replace' attribute from home.html, it will display the welcome message.
Browsing similar questions (e.g. this one), I see other people create ThymeleafViewResolver bean.
Questions:
Why can't it find my templates?
Do I need to create a template resolver? If so, how come the pet clinic sample doesn't? Where does its template resolver come from?
As far as I can see it's set up the same way as the pet clinic sample, so what's the difference?
解决方案
Spring boot does some internal works automagically for you, but to do that automagical work you need to configure it properly. This is the answer for your question 2, spring automagically provides the view resolver if configuration is right. Ok, now lets move to question 1.
If you compare your pom.xml with that of petclinic, You'll see some key differences.
With that setup, you can also override individual dependencies by overriding a property in your own project. For instance, in petclinic pom.xml, they specified the thymeleaf version which is missing in yours.
Now to properly setup Thymeleaf and Thymeleaf layout dialect version with compatibility between them you need to exclude thymeleaf-layout-dialect from spring-boot-starter-thymeleaf, bcz of setting thymeleaf version to 3.0.2.RELEASE version.