【问题标题】:Thymeleaf +Spring Boot can't find files (FileNotFoundException)Thymeleaf +Spring Boot 找不到文件(FileNotFoundException)
【发布时间】:2018-03-21 17:47:29
【问题描述】:
这有点愚蠢和令人沮丧:
@Configuration 取自教程网站或论坛,其中包含一个
ServletContextTemplateResolver thymeleafTemplateResolver
是使用 spring boot 提供的 ServletContext 创建的。
请求时,尽管文件位于配置的资源文件夹中,但仍会引发 FileNotFoundException。
我如何让它找到文件/从资源中加载它?
【问题讨论】:
标签:
spring-boot
thymeleaf
filenotfoundexception
【解决方案1】:
为了让thymeleaf解析classpath资源,你需要配置一个ClassLoaderTemplateResolver。 (您使用的是ServletContextTemplateResolver)
还要检查 setPrefix 是否设置为正确的文件夹,例如。 “/thymeleaf/”,如果您的文档位于 resources/thymeleaf/ 中并且 setSuffix 设置为“.html”(或任何您喜欢的文件后缀)
要同时提供静态内容,您可以扩展 WebMvcConfigurer 并覆盖 addResourceHandlers,然后执行例如
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
假设您的资源中有一个静态文件夹。
(这里优先使用 Spring 控制器)