我在Heroku上成功部署了我的Spring应用程序,但是当我尝试测试我的应用程序时,我收到了
Error resolving template "/fragments/head.html", template might not exist or might not be accessible by any of the configured Template Resolvers (home:4)
错误。
这很奇怪,因为本地我的应用程序运行良好没有任何问题。
Home.html代码:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<div th:replace="/fragments/head.html"></div>
<title>Personal blog - Home</title>
</head>
<body>
<div class="container">
<div th:replace="/fragments/header" th:class="row"></div>
<div class="container">
<div class="container border w-100 post" th:each="post : ${posts}">
<div class="container">
<h1 class="display-5">
<a th:href="@{/post/} + ${post.id}" th:text="${post.title}"></a>
</h1>
<small class="text-muted" th:text="${post.creationDate}"></small>
</div>
<div class="container">
<p class="lead text-justify" th:text="${post.contents}"></p>
</div>
</div>
<nav>
<ul class="pagination justify-content-center" th:if="${lastPageIndex} gt 1">
<li class="page-item">
<a class="page-link"
th:if="${currentPage gt 1}"
th:href="@{/home/} + ${currentPage-1}">Previous</a>
</li>
<li class="page-item">
<a class="page-link" th:if="${currentPage-3} ge 1"
th:href="@{/home/} + ${currentPage-3}"
th:text="${currentPage-3}"></a>
</li>
<li class="page-item">
<a class="page-link"
th:if="${currentPage-2} ge 1"
th:href="@{/home/} + ${currentPage-2}"
th:text="${currentPage-2}"></a>
</li>
<li class="page-item">
<a class="page-link"
th:if="${currentPage-1} ge 1"
th:href="@{/home/} + ${currentPage-1}"
th:text="${currentPage-1}"></a>
</li>
<li class="page-item">
<a class="page-link" th:text="${currentPage}"></a>
</li>
<li class="page-item">
<a class="page-link"
th:if="${currentPage+1} le ${lastPageIndex}"
th:href="@{/home/} + ${currentPage+1}"
th:text="${currentPage+1}"></a>
</li>
<li class="page-item">
<a class="page-link"
th:if="${currentPage+2} le ${lastPageIndex}"
th:href="@{/home/} + ${currentPage+2}"
th:text="${currentPage+2}"></a>
</li>
<li class="page-item">
<a class="page-link"
th:if="${currentPage+3} le ${lastPageIndex}"
th:href="@{/home/} + ${currentPage+3}"
th:text="${currentPage+3}"></a>
</li>
<li class="page-item">
<a class="page-link"
th:if="${currentPage} lt ${lastPageIndex}"
th:href="@{/home/} + ${currentPage+1}">Next</a>
</li>
</ul>
</nav>
</div>
<div th:replace="/fragments/footer"></div>
</div>
</body>
</html>
Head.html代码:
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org">
<head>
<div th:fragment="head">
<meta charset="UTF-8"/>
<link rel="stylesheet" type="text/css" href="/css/style.css"/>
<link rel="stylesheet" type="text/css" crossorigin="anonymous"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"
integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb"/>
</div>
</head>
</html>
正如您所看到的,我的Head.html包含指向CSS文件的链接,一个包含引导程序,第二个包含我自己的修复程序。在当地,一切正常。每个视图都完美加载我在加载页面和应用css之间只有一点半的延迟,但我认为在heroku上它会很好。
我的aspp在此链接下可用:https://spring-personal-blog.herokuapp.com
我的应用程序的整个代码也可以在github上的repository中找到。
答案 0 :(得分:1)
您的语法无效th:replace
。应该使用
th:replace="fragments/head :: head"
// or
th:replace="fragments/head" // instead of th:replace="/fragments/head"
th:replace="directory/filename :: fragmentName"
模板中每个其他片段的类似更改
<div>
内的 <head>
,您应该使用th:remove="tag"
。
//home.html
<div th:replace="fragments/head :: head" th:remove="tag"></div>
//fragments/head.html
<head th:fragment="head">...</head>
PS。我玩你的github项目,有点奇怪,你的符号实际上是在localhost上工作但在heroku上休息。以上修复程序在heroku DEMO上为我解决了这个问题。顺便说一下,我还必须将passwordEncoder
bean放在不同的配置文件中,因为它无法在heroku上启动,否则