Java Thymeleaf:错误解决模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29318811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thymeleaf: Error Resolving Template
提问by jkratz55
I am trying to use layouts/templates with Thymeleaf but I'm getting the following exception.
我正在尝试将布局/模板与 Thymeleaf 一起使用,但出现以下异常。
Exception processing template "user/index": Error resolving template "/layouts/default.html", template might not exist or might not be accessible by any of the configured Template Resolvers
异常处理模板“user/index”:解析模板“/layouts/default.html”时出错,模板可能不存在或可能无法被任何配置的模板解析器访问
Here is my ThymeleafConfig.java
这是我的 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;
}
}
I have the following folder structure
我有以下文件夹结构
webapp/
..WEB-INF/
....views/
......layouts/
........default.html
......user
........index.html
Here is my default.html which is my main layout.
这是我的 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>
Here is the index.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>
They are in different folders but the pathing should work unless I'm just missing something really silly.
它们位于不同的文件夹中,但路径应该可以工作,除非我只是遗漏了一些非常愚蠢的东西。
采纳答案by jkratz55
I found my issue. If you specify the suffix in your Thymeleaf config you do not need the .html extension.
我发现了我的问题。如果您在 Thymeleaf 配置中指定后缀,则不需要 .html 扩展名。
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html"); // here
resolver.setTemplateMode("HTML5");
resolver.setOrder(1);
return resolver;
}
It should be:
它应该是:
layout:decorator="layouts/default"
Instead of:
代替:
layout:decorator="layouts/default.html"
I'm guessing it was effectively looking for layouts/default.html.html which would be a problem.
我猜它正在有效地寻找 layouts/default.html.html 这将是一个问题。
回答by love kumar
You should not give the extension.And you have to provide correct path like this
你不应该给扩展名。你必须像这样提供正确的路径
layout:decorator="../layouts/default"
layout:decorator="../layouts/default"
回答by Shehan Simen
I solved this issue by having following code:
我通过以下代码解决了这个问题:
@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;
}
}
}
The trick was to add: emailTemplateResolver.setPrefix("classpath:/templates/");
诀窍是添加: emailTemplateResolver.setPrefix("classpath:/templates/");