我的Spring启动应用程序中有以下项目结构,我想使用Thymeleaf
projectName
-Gradle-Module1(Spring boot module)
-build
-src
-main
-resources
-templates
index.html
build.gradle
-Gradle-Module2
...
build.gradle
...
但是spring-boot找不到我的模板目录并显示警告
Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
PS:我正在使用 @EnableAutoConfiguration
在我的控制器代码中,我正在做类似的事情
@Controller
@EnableAutoConfiguration
public class BaseController {
@RequestMapping(value = "/")
public String index() {
return "index.html";
}
}
和 index.html
文件只打印hello world .
所以通常它应该在 src/resources/templates/
内部(我认为是相同的Gradle模块),但不知何故它无法找到它 .
当我尝试访问 localhost:8080
时我收到以下错误 Error resolving template "index.html", template might not exist or might not be accessible by any of the configured Template Resolvers
我有什么遗漏吗?
任何指针赞赏 .
谢谢 .
3 回答
您应该只返回文件名 . 例如没有.hmtl
您必须按如下方式配置Thymeleaf:
Spring doc recommends将
@EnableAutoConfiguration
注释添加到主@Configuration
类 .此外,您似乎有错误的项目结构,典型的包层次结构是:
在这种情况下,您的模板将位于
src/main/resources/templates
中,而不是src/resources/templates/
中 .