springboot项目无法访问静态页面的问题
19级学生蒋涵屿静态资源访问最近在做SpringBoot项目的时候遇到了“白页”问题,通过查资料和问老师对SpringBoot访问静态资源做了如下总结。Spring Boot 访问静态资源在 SpringBoot 项目中没有我们之前常规 web 开发的WebContent(WebApp),它只有 src 目录。在 src/main/resources 下面有两个文件夹,static 和 temp
·
19级学生蒋涵屿
静态资源访问
最近在做SpringBoot项目的时候遇到了“白页”问题,通过查资料和问老师对SpringBoot访问静态资源做了如下总结。
Spring Boot 访问静态资源
在 SpringBoot 项目中没有我们之前常规 web 开发的WebContent(WebApp),它只有 src 目录。在 src/main/resources 下面有两个文件夹,static 和 templates。 SpringBoot 默认在 static 目录中存放静态页面,而 templates 中放动态页面。
1 static 目录
Spring Boot 通过 classpath/static 目录访问静态资源。注意存放静态资源的目录名称必须是 static。
-
将静态资源放在此目录下,通过浏览器直接可以访问
-
也可以通过Controller访问
2 templates 目录
在 Spring Boot 中一般使用 jsp 作为视图层技术,而是默认使用 Thymeleaf 来做动态页面。Templates 目录这是存放 Thymeleaf 的页面。 如果要想在Templates目录下面访问到html页面,可以用下面这段代码
3 静态资源存放其他位置
3.1 Spring Boot 访问静态资源的位置(优先级按以下顺序) classpath默认就是resources,所以classpath:/static/ 就是resources/static/
classpath:/META‐INF/resources/ classpath:/resources/ classpath:/static/ classpath:/public/
3.2 自定义静态资源位置
配置静态资源访问路径
spring.web.resources.static-locations=classpath:/haha/
更多推荐
已为社区贡献1条内容
所有评论(0)