java Thymeleaf 无法检测到 spring-boot 项目中的模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42947454/
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 cannot detect templates inside spring-boot project
提问by Vihar
I have the following project structure in my Spring boot app, in which I want to use Thymeleaf
我的 Spring boot 应用程序中有以下项目结构,我想在其中使用 Thymeleaf
projectName
-Gradle-Module1(Spring boot module)
-build
-src
-main
-resources
-templates
index.html
build.gradle
-Gradle-Module2
...
build.gradle
...
but the spring-boot cannot find my template directory and is showing warning
但 spring-boot 找不到我的模板目录并显示警告
Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
PS: I am using @EnableAutoConfiguration
PS:我正在使用 @EnableAutoConfiguration
In my controller code I am doing something like
在我的控制器代码中,我正在做类似的事情
@Controller
@EnableAutoConfiguration
public class BaseController {
@RequestMapping(value = "/")
public String index() {
return "index.html";
}
}
and index.html
file just prints hello world.
和index.html
文件只打印你好世界。
so typically it should look inside src/resources/templates/
(of same Gradle module I suppose) , but somehow it is not able to find it.
所以通常它应该查看内部src/resources/templates/
(我想是同一个 Gradle 模块),但不知何故它无法找到它。
when I try to access localhost:8080
I am getting below error Error resolving template "index.html", template might not exist or might not be accessible by any of the configured Template Resolvers
Is there anything I am missing?
当我尝试访问localhost:8080
时出现以下错误Error resolving template "index.html", template might not exist or might not be accessible by any of the configured Template Resolvers
有什么我遗漏的吗?
Any pointers appreciated.
任何指针表示赞赏。
Thanks.
谢谢。
回答by DimaSan
You have to configure Thymeleaf as follows:
您必须按如下方式配置 Thymeleaf:
@Configuration
public class ThymeleafConfig {
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setCacheable(false);
templateResolver.setPrefix("classpath:/templates/");
templateResolver.setSuffix(".html");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
springTemplateEngine.addTemplateResolver(templateResolver());
return springTemplateEngine;
}
@Bean
public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setOrder(1);
return viewResolver;
}
}
Spring doc recommendsto add @EnableAutoConfiguration
annotation to your primary @Configuration
class.
Spring doc 建议为@EnableAutoConfiguration
您的主@Configuration
类添加注释。
Also it seems you have wrong project structure, the typical package hierarchy is:
此外,您似乎有错误的项目结构,典型的包层次结构是:
src
|- main
|- java
|- resources
|- static
|- templates
|- test
In this case your templates will be in src/main/resources/templates
, not in src/resources/templates/
.
在这种情况下,您的模板将src/main/resources/templates
在src/resources/templates/
.
回答by kkflf
You should only return the file name. E.g without the .hmtl
您应该只返回文件名。例如没有 .hmtl
@RequestMapping(value = "/")
public String index() {
return "index";
}
回答by Ashraful041
@GetMapping("/")
public String index() {
return "index";
}