Spring Boot + Thymeleaf CSS 文件无法访问,并且有 500 错误,找不到模板



当我使用 Spring Boot 1.4.0 + Thymeleaf 时,我发现静态资源无法访问并抛出错误"模板可能不存在或可能无法被任何配置的模板解析程序访问"。

关于我的静态资源的文件夹结构

浏览器显示 500 错误

从服务器日志中,可以找到错误"找不到模板"。但是 URL 应该是我的 CSS 文件位置。

2018-08-28 21:07:29.826 ERROR 5676 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "Spring Boot Application is available.", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause

听起来 Spring Boot 正在尝试将资源路径解析为模板。 可能与指向其中一个控制器的路径存在冲突。 默认情况下,Spring 引导提供来自/**的静态资源。 您可以使用spring.mvc.static-path-pattern更改application.properties文件中的此默认路径。

## application.properties
spring.mvc.static-path-pattern=/resources/**

这将导致静态内容的新资源路径

Boostrap.min.css

http://localhost:8080/resources/css/bootstrap.min.css

https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-spring-mvc-static-content

检查我的源代码后,我发现这是因为我有另一个控制器请求映射到主页并阻止 CSS/JS URL。 在我评论函数test2((之后,主页现在可以加载静态资源了。

@RequestMapping(value = "/")
public String test1(){
return "index";
}
@RequestMapping
public String test2(){
return "index";
}

相关内容

最新更新