【发布时间】:2015-03-16 12:21:46
【问题描述】:
我正在尝试在我当前的 spring-boot 项目中使用 thymeleaf 页面布局。我将此添加到我的 index.html 视图中(放置在资源/模板/公共中):
<nav class="navbar navbar-default navbar-fixed-top">
<div th:include="menu :: menu">...</div>
</nav>
视图 menu.html 是这样的:
<!DOCTYPE html>
<html>
<head></head>
<body>
<div class="container" th:fragment="menu">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" th:href="@{/}">Project name</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
</ul>
<ul class="nav navbar-nav navbar-right">
<li sec:authorize="isAnonymous()"><a th:href="@{/signin}">Entrar</a></li>
<li sec:authorize="isAnonymous()"><a th:href="@{/signup}">Cadastro</a></li>
<li sec:authorize="isAuthenticated()"><a th:href="@{/logout}">Logout</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</body>
</html>
但是当我运行项目时,我收到了这个错误:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "menu", template might not exist or might not be accessible by any of the configured Template Resolvers (public/index:77)] with root cause
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "menu", template might not exist or might not be accessible by any of the configured Template Resolvers (public/index:77)
我的 application.properties 文件中有 thymeleaf 的此配置:
# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
我也有这个配置类:
@Configuration
public class ThymeleafContext {
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addTemplateResolver(templateResolver());
final Set<IDialect> dialects = new HashSet<IDialect>();
dialects.add( new SpringSecurityDialect() );
engine.setDialects( dialects );
return engine;
}
public ITemplateResolver templateResolver() {
TemplateResolver resolver = new TemplateResolver();
return resolver;
}
}
我在这里缺少什么?
【问题讨论】:
-
您的 menu.html 与 index.html 位于同一文件夹中还是在子文件夹中?在后者中,您需要将子文件夹添加到 th:include。喜欢子文件夹/菜单::菜单
-
@MystyxMac 它在同一个文件夹中。
-
即使它们在同一个文件夹中,我认为 Thymeleaf 从前缀开始搜索(在本例中为 /templates/,而 menu.html 在 //templates/public。你可以试试公共/菜单 :: 菜单?
标签: spring layout spring-boot thymeleaf