当前位置:网站首页>8. WEB 开发-静态资源访问
8. WEB 开发-静态资源访问
2022-08-10 23:35:00 【要学就学灰太狼】
8.1 官方文档
- 在线文档 ==> 官方文档
8.2 基本介绍
- 只要静态资源放在类路径下: /static 、 /public 、 /resources 、 /META-INF/resources 可以被直接访问- 对应文件 WebProperties.java
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
- 常见静态资源:JS、CSS 、图片 (.jpg .png .gif .bmp .svg)、字体文件 (Fonts) 等
- 访问方式:默认: 项目根路径/ + 静态资源名 比如 http://localhost:8080/hi.jpg - 设置 WebMvcProperties.java
/** * Path pattern used for static resources. */
private String staticPathPattern = "/**";
8.3 快速入门
- 创建 SpringBoot 项目 springbootweb ,使用灵活配置方式来创建项目
- 创建相关静态资源目录,并放入测试图片,没有目录,自己创建即可,完成测试
----------浏览器测试结果如下----------
8.4 静态资源访问注意事项和细节
- 静态资源访问原理:静态映射是 /** ,也就是对所有请求拦截,请求进来,先看 Controller 能不能处理,不能处理的请求交给静态资源处理器,如果静态资源找不到则响应 404 页面
- 改变静态资源访问前缀,比如我们希望 http://localhost:8080/xjsres/* 去请求静态资源,应用场景:静态资源访问前缀和控制器请求路径冲突
- 创建 D:\xjs_springboot\springbootweb\src\main\resources\application.yml
spring:
mvc:
static-path-pattern: /xjsres/**
- 重启应用,完成测试,浏览器输入: http://localhost:8080/xjsres/1.jpg
- 改变默认的静态资源路径,比如希望在类路径下增加 xjsimg 目录 作为静态资源路径,并完成测试
- 如图所示
- 配置 D:\xjs_springboot\springbootweb\src\main\resources\application.yml
spring:
mvc:
static-path-pattern: /xjsres/** #修改静态资源访问的路径/前缀
web:
resources:
#修改/指定 静态资源的 存放路径/位置
#注意:配置了 static-locations 原来的访问路径就被覆盖了,如果需要保留,要再指定一下
static-locations: ["classpath:/xjsimg/", "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/"] # String[] staticLocations
测试:
- 浏览器输入:http://localhost:8080/xjsres/6.jpg
- 另外因为 6.jpg 还是拷贝来的,一定要保证工作目录 target 有 6.jpg
- 如果没有,请 rebulid 下项目,再重启项目
- 如果你配置 static-locations,原来的访问路径就被覆盖,如果需要保留,你再指定一下即可
边栏推荐
- CSAPP lab
- 【C语言】猜数字游戏的实现
- 企业小程序怎么开发自己的小程序?
- C language% (%d,%c...)
- 【C语言】二分查找(折半查找)
- 矩阵的迹(详解)
- 数组 冒泡排序
- 从Todolist入门Svelte框架
- [C language] First understanding of pointers
- There is no recycle bin for deleted files on the computer desktop, what should I do if the deleted files on the desktop cannot be found in the recycle bin?
猜你喜欢
随机推荐
ROS Experiment Notes - Validation of UZH-FPV Dataset
How to quickly grasp industry opportunities and introduce new ones more efficiently is an important proposition
卷积神经网络CNN详细介绍
HPb59-1铅黄铜
Microsoft: Into Focus with Scott Guthrie Scott Hanselman Rajesh Jha and Kevin Scott | KEY11
vr虚拟仿真教学系统应用场景开发
【ORACLE】什么时候ROWNUM等于0和ROWNUM小于0,两个条件不等价?
2022牛客多校(七)K. Great Party博弈方法证明
服务器小常识
多语种翻译-多语种翻译软件免费
How to recover data from accidentally deleted U disk, how to recover deleted data from U disk
企业小程序怎么开发自己的小程序?
【C语言】C语言程序设计:动态通讯录(顺序表实现)
[C language] binary search (half search)
call,apply,bind指定函数的this指向详解,功能细节,严格和非严格模式下设定this指向
ROS实验笔记之——UZH-FPV数据集的验证
[C language articles] Expression evaluation (implicit type conversion, arithmetic conversion)
好用的翻译插件-一键自动翻译插件软件
[C language] Implementation of guessing number game
三栏布局实现