我正在尝试使用Thymeleaf的布局/模板,但我得到以下异常。
异常处理模板"用户/索引":解析模板错误" /layouts/default.html",模板可能不存在,或者任何已配置的模板解析器都可能无法访问
这是我的ThymeleafConfig.java
@Configuration
public class ThymeleafConfig {
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setOrder(1);
return resolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
engine.addDialect(new LayoutDialect());
engine.addDialect(new SpringSecurityDialect());
engine.addDialect(new SpringStandardDialect());
return engine;
}
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
return resolver;
}
}
我有以下文件夹结构
webapp/
..WEB-INF/
....views/
......layouts/
........default.html
......user
........index.html
这是我的default.html,这是我的主要布局。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Default</title>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<header>
This is a header from default.html
</header>
<section layout:fragment="content">
<p>Content should go here!</p>
</section>
<footer>
Footer from default
<p layout:fragment="custom-footer">Custom footer here!</p>
</footer>
<!-- scripts -->
<script src="https://code.jquery.com/jquery-2.1.3.min.js" />
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
</body>
</html>
这是index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="layouts/default.html">
<head>
<title>Users</title>
</head>
<body>
<section layout:fragment="content">
<p>This is a paragraph from content page 1</p>
</section>
<footer>
<p layout:fragment="custom-footer">This is some footer content from content page 1</p>
</footer>
</body>
</html>
他们在不同的文件夹中,但路径应该有效,除非我错过了一些非常愚蠢的东西。
答案 0 :(得分:8)
我发现了我的问题。如果在Thymeleaf配置中指定后缀,则不需要.html扩展名。
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/views/");
**resolver.setSuffix(".html");**
resolver.setTemplateMode("HTML5");
resolver.setOrder(1);
return resolver;
}
应该是:
layout:decorator="layouts/default"
而不是:
layout:decorator="layouts/default.html"
我猜它有效地寻找layouts / default.html.html,这将是一个问题。
答案 1 :(得分:1)
我通过以下代码解决了这个问题:
@Configuration
public class ThymeleafConfig{
@Bean
public SpringTemplateEngine springTemplateEngine()
{
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(htmlTemplateResolver());
return templateEngine;
}
@Bean
public SpringResourceTemplateResolver htmlTemplateResolver()
{
SpringResourceTemplateResolver emailTemplateResolver = new SpringResourceTemplateResolver();
emailTemplateResolver.setPrefix("classpath:/templates/");
emailTemplateResolver.setSuffix(".html");
emailTemplateResolver.setTemplateMode(StandardTemplateModeHandlers.HTML5.getTemplateModeName());
emailTemplateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
return emailTemplateResolver;
}
}
诀窍是添加:emailTemplateResolver.setPrefix("classpath:/templates/");
答案 2 :(得分:0)
你不应该给予扩展名。你必须提供这样的正确路径
布局:装饰=&#34; ../布局/默认&#34;