-
2022-01-01 21:58:24
在SpringBoot项目中没有我们之前常规 web开发的WebContent ( WebApp),它只有src目录。
在src/main/resources下面有两个文件夹, static和 templates. SpringBoot默认在 static目录中存放静态页面,而templates中放动态页面。
classpath : --> src/main/resources/
static目录
Spring Boot通过 c l a s s p a t h : / s t a t i c \color{red}{classpath : /static } classpath:/static目录访问静态资源。注意存放静态资源的目录名称必须是static。
templates目录
在Spring Boot中不推荐使用jsp作为视图层技术,而是默认使用Thymeleaf 来做动态页面。Templates目录这是存放Thymeleaf 的页面。
SpringBoot 静态资源存放其他位置
c l a s s p a t h : / M E T A − I N F / r e s o u r c e s / \color{red}{classpath:/META - INF/resources/ } classpath:/META−INF/resources/
c l a s s p a t h : / r e s o u r c e s / \color{red}{classpath:/resources/} classpath:/resources/
c l a s s p a t h : / s t a t i c \color{red}{classpath:/static } classpath:/static
c l a s s p a t h : / p u b l i c / \color{red}{classpath:/public/ } classpath:/public/spring: resources: static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
自定义 静态资源存放其他位置
spring: resources: static-locations: classpath:/自定义目录/,[classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/]
更多相关内容 -
SpringBoot访问静态资源
2022-04-03 18:21:33SpringBoot中存访问静态资源SpringBoot-----SpringBoot访问静态资源
🔥一、静态资源相关目录
SpringBoot项目中没有WebApp目录,只有src目录。在src/main/resources下面有static和templates两个文件夹。SpringBoot默认在static目录中存放静态资源,而templates中放动态页面。
1、static目录
SpringBoot通过/resources/static目录访问静态资源,在resources/static中编写html页面:
2、templates目录
在SpringBoot中不推荐使用JSP作为动态页面,而是默认使用Thymeleaf编写动态页面。templates目录是存放Thymeleaf页面的目录,稍后我们讲解Thymeleaf技术。
🔥二、静态资源其他存放位置
除了/resouces/static目录 , SpringBoot还会扫描以下位置的静态资源:
1 、/resources/META-INF/resources/
2、/resources/resources/
3、resources/public/
在SpringBoot配置文件进行自定义静态资源位置配置:
spring:
web:
resources:
static-locations: classpath:/yingxiong/,classpath:/static/注意:
该配置会默认覆盖静态资源位置 , 如果还想使用之前的静态资源位置 , 还需要配置在后面
SpringBoot2.5之前的配置方式为 spring.resources.static-locations✨脚踏实地,一步一步,总能成功✨
-
springboot访问静态资源
2022-04-10 14:27:20访问 : 当前项目根路径/ + 静态资源名 原理: 静态映射/**。 请求进来,先去找Controller看能不能处理。不能处理的所有请求又都交给静态资源处理器。静态资源也找不到则响应404页面 改变默认的静态资源路径 spring...1、静态资源目录
只要静态资源放在类路径下:
called /static (or /public or /resources or /META-INF/resources
访问 : 当前项目根路径/ + 静态资源名
原理: 静态映射/**。
请求进来,先去找Controller看能不能处理。不能处理的所有请求又都交给静态资源处理器。静态资源也找不到则响应404页面改变默认的静态资源路径
spring: mvc: static-path-pattern: /res/** resources: static-locations: [classpath:/haha/]
2、静态资源访问前缀
默认无前缀
spring: mvc: static-path-pattern: /res/**
当前项目 + static-path-pattern + 静态资源名 = 静态资源文件夹下找
**
目录结构
**
实例
-
springboot访问静态资源方法
2022-04-16 10:29:47这样改之后可以防止与Controller中的访问冲突 #resources: #static-locations: [classpath:/haha/] #这是改变静态资源的存放路径 #add-mappings: true #为true表示可以用上面提到的规则直接访问静态资源,...这是将vue项目放到静态包下
目录结构端口和前缀
vue.html
<!DOCTYPE html> <!--suppress ALL --> <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>vxe Demo</title> <meta name="description" content="Element UI Demo"> <link rel="stylesheet" th:href="@{/assets/persional/views/dist/css/app.6aa77349.css}"/> <link rel="stylesheet" th:href="@{/assets/persional/views/dist/css/chunk-vendors.59514fc7.css}"/> <p>fdsfdsfdsgfdsgfdghfdshfdgfdgdf</p> </head> <body> <script defer="defer" type="module" th:src="@{/assets/persional/views/dist/js/chunk-vendors.42ad1716.js}"></script> <script defer="defer" type="module" th:src="@{/assets/persional/views/dist/js/app.105eb1f3.js}"></script> <script defer="defer" th:src="@{/assets/persional/views/dist/js/chunk-vendors-legacy.c0641cf1.js}" nomodule></script> <script defer="defer" th:src="@{/assets/persional/views/dist/js/app-legacy.b0780305.js}" nomodule></script> <div id="app"></div> </body> </html>
方法一
templates与static包同级,记住,否则找不到页面500异常
添加依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
在controller_view中
@RestController @RequestMapping("/view") public class IndexView { @GetMapping("/index1") public ModelAndView login1() { return new ModelAndView("/views/vue"); //路径只需要填写html名字,不需要填写index.html,只需要填写index } }
在yml中
spring: thymeleaf: suffix: .html # 过滤所有非html结尾的文件 classpath: /templates/model/** #则不指定suffix: .html --> 扫描/templates/model/下所有文件 prefix: classpath:/templates/model/ # 扫描/templates/model/下所有文件 cache: false # 关闭缓存 开发模式下设置为false,避免改了模板还要重启服务器,线上设置为true,可以提高性能。 encoding: UTF-8 check-template-location: true #check-tempate-location: 检查模板路径是否存在
请求成功
方法二
与方法一有所不同吗,当templates在static下时
yml中配置如下
spring: mvc: static-path-pattern: /sta/** #配置静态资源的访问前缀:改了之后想要直接加载静态资源要用http://localhost:端口号/sta/静态资源名。 这个sta是虚拟的,并不是真实存在的路径,这样改之后可以防止与Controller中的访问冲突 #resources: #static-locations: [classpath:/haha/] #这是改变静态资源的存放路径 #add-mappings: true #为true表示可以用上面提到的规则直接访问静态资源,false时表示禁止所有静态资源规则
成功访问http://localhost:58080/seckill/sta/templates/model/views/vue.html
方法三
承接方法一,写一个ModelAndView
@RestController @RequestMapping("/view") public class IndexView { @GetMapping("/index") public ModelAndView login() { return new ModelAndView("/sta/templates/model/views/vue.html"); } }
访问路径http://localhost:58080/seckill/view/index
成功了
引入的JS、CSS文件
方法四
直接访问
@Controller @RequestMapping("/view") public class IndexView { @GetMapping("/index") public String login() { return "/templates/model/views/vue.html"; } }
若是配置了mvc
mvc: static-path-pattern: /sta/** #配置静态资源的访问前缀:改了之后想要直接加载静态资源要用http://localhost:端口号/sta/静态资源名。 这个ers是虚拟的,并不是真实存在的路径,这样改之后可以防止与Controller中的访问冲突
则
@Controller @RequestMapping("/view") public class IndexView { @GetMapping("/index") public String login() { return "/sta/templates/model/views/vue.html"; } }
成功
但是,如果加了依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
并且配置了
#thymeleaf: #suffix: .html # 过滤所有非html结尾的文件 #classpath: /templates/model/** #则不指定suffix: .html --> 扫描/templates/model/下所有文件 #prefix: classpath:/templates/model/ # 扫描/templates/model/下所有文件 #cache: false # 关闭缓存 开发模式下设置为false,避免改了模板还要重启服务器,线上设置为true,可以提高性能。 #encoding: UTF-8 #check-template-location: true #check-tempate-location: 检查模板路径是否存
将会报错
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.Fri Apr 29 11:18:36 CST 2022
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [/sta/templates/model/views/vue.html], template might not exist or might not be accessible by any of the configured Template Resolvers -
springBoot访问静态资源
2021-04-13 14:54:09关于spring boot如何访问静态资源 1、在resources文件夹下创建static文件夹 在static文件夹下可以创建静态文件 在static文件夹下的静态资源可以直接在浏览器中访问 如 访问html文件 2、设置欢迎页... -
SpringBoot访问静态资源(图片)
2022-04-27 07:39:14springboot访问静态资源的几种方式 (优先级从高到低) (1)在src/main/resources/目录下创建 META-INF/resources文件夹 (2)在src/main/resources/目录下创建 resources文件夹 (3)在src/main/resources/目录下创建 ... -
SpringBoot访问静态资源出现404
2021-11-18 15:24:54SpringBoot访问静态资源出现404 SpringBoot图片路径找不到 WebMvcConfigurer -
SpringBoot访问静态资源报404——记录一次调试过程与解决方案
2022-04-27 15:53:34SpringBootWeb项目中,默认的静态资源路径有下列4种: classpath:/META-INF/resources classpath:/resources classpath:/static classpath:/public** -
springboot 访问静态资源
2020-06-17 15:10:54问题点:sprignboot项目创建时默认自动创建静态资源文件夹resources/static和resources/templates,默认可直接访问静态资源路劲有四类: /static,/public,/resources,/META-INF/resources。但是我现在想先访问... -
SpringBoot访问静态资源失败解决方案
2021-04-19 17:10:07新建一个项目访问静态资源一点毛病没有,这就有点意思:: 看一下我的静态资源目录,多捡漏啊=-= 首先、检查classes目录 小白遇到这种问题一般可以先查看一下classes目录(问题大多出在这上面),检查SpringBoot有... -
简单快速的用SpringBoot访问静态资源(图片、html)
2022-04-12 14:34:45首先需要记载Springboot访问静态资源的Jar文件 org.springframework.boot spring-boot-starter-thymeleaf 还需要在“resources”目录下(注意只能在“resources”目录下创建文件,因为SpringBoot只在它下面扫描)... -
SpringBoot访问静态资源文件(css、js、images)
2021-07-29 12:01:41在做SpringBoot访问静态资源文件(css、js、images)时候,发现总是报错: 以为是路径中没有添加static,所以路径又添加static,再次访问如下: 额哦,还是一样的错误,看来不是这个问题。继续查找原因,发现... -
SpringBoot访问静态资源中文乱码
2021-06-15 14:22:42SpringBoot的静态资源位置如下几个: /META-INF/resources/ classpath:/resources/ classpath:/static/ classpath:/public/ 优先级从上之下 逐渐降低。 访问其中的资源文件可能会出中文乱码: 解决方式: 在... -
SpringBoot访问静态资源.docx
2022-06-27 21:17:17启动项目,访问http://localhost:8080/0101.jpg 可以访问! 也可以在static中建立别的文件夹,例如:images ,访问的时候需要加上... 如果想要在html页面中访问静态资源(比如图片),那么只需要填写相对路径即可例如: -
springboot访问静态资源两种情况
2020-03-07 10:46:051.springboot自身访问静态资源 2.springboot结合第三方模版引擎访问静态资源 先贴上一个项目 springboot自身访问静态资源 1.在springboot项目中,访问静态资源是怎么配置的?哪里体现? springboot默认访问静态资源路径... -
Springboot访问静态资源文件的html以及图片
2022-04-06 12:25:00想要访问静态资源文件需要先导入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency&g -
SpringBoot——静态资源访问的四种方式
2022-05-21 14:09:341.默认的静态资源目录 /static /public /resources /META-INF/resources ...3.1.通过路径访问静态资源 http://localhost:8080/a.jpg http://localhost:8080/b.jpg http://localhost:8080/c.png htt... -
SpringBoot静态资源目录访问
2020-08-26 12:16:50今天小编就为大家分享一篇关于SpringBoot静态资源目录访问,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧