我正在尝试在我当前的spring-boot项目中使用百万美元页面布局。我将它添加到我的index.html视图(放在resources / templates / public中):
<nav class="navbar navbar-default navbar-fixed-top">
<div th:include="menu :: menu">...</div>
</nav>
view 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;
}
}
我在这里缺少什么?
答案 0 :(得分:0)
错误只是说明您没有指定模板文件位置的menu.html
。我通常更喜欢XML配置而不是属性文件。检查它是否确实应包含classpath
spring.thymeleaf.prefix
的引用。以下是我在mvc-dispatcher.xml
上使用的配置,用于指定Thymeleaf参数。据我所知,您的配置类似,而不是XML:
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false" />
</bean>
正如您所看到的,我没有引用classpath
,而只是引用我的WEB-INF
文件夹。尝试更改为XML并确保您的menu.html
不在子文件夹中。
OBS :小心在片段上指定一个css类,Thymeleaf可能无法加载它,因为它会加载片段内的内容,而不是正在被引用的实际div,意味着你的{ {1}}夜晚只是被忽略了。替代方案是在包含片段的div中设置类,或者将整个片段封装在外部dif上,然后将其命名为片段。