Springboot靜態(tài)資源的訪問方法介紹
1.官方文檔
2.基本介紹
只要靜態(tài)資源放在類路徑下: /static 、 /public 、 /resources 、 /META-INF/resources 可以被直接訪問- 對應(yīng)文件 WebProperties.java
直接放到resources目錄下是訪問不到的,這里的 /resources是指在resource目錄的創(chuàng)建resources目錄
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
常見靜態(tài)資源:JS、CSS 、圖片(.jpg .png .gif .bmp .svg)、字體文件(Fonts)等
訪問方式 :默認: 項目根路徑/ + 靜態(tài)資源名 比如 http://localhost:8080/hi.jpg . - 設(shè)置 WebMvcProperties.java
/** * Path pattern used for static resources. */ private String staticPathPattern = "/**";
3.快速入門
1.創(chuàng)建 SpringBoot 項目 springbootweb
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.llp</groupId> <artifactId>springBootweb</artifactId> <version>1.0-SNAPSHOT</version> <!--導(dǎo)入springboot父工程-規(guī)定寫法--> <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.5.3</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
2.創(chuàng)建相關(guān)靜態(tài)資源目錄, 并放入測試圖片, 沒有目錄,自己創(chuàng)建即可, 完成測試
4.靜態(tài)資源訪問注意事項和細節(jié)
靜態(tài)資源訪問原理:靜態(tài)映射是 /**, 也就是對所有請求攔截,請求進來,先看 Controller 能不能處理,不能處理的請求交給靜態(tài)資源處理器,如果靜態(tài)資源找不到則響應(yīng) 404 頁面
改變靜態(tài)資源訪問前綴,比如我們希望 http://localhost:8080/llp/* 去請求靜態(tài)資源, 應(yīng)用場景:靜態(tài)資源訪問前綴和控制器請求路徑?jīng)_突
(1)創(chuàng)建src\main\resources\application.yml
spring:
mvc:
static-path-pattern: /llp/**
(2)重啟應(yīng)用,完成測試, 瀏覽器輸入: http://localhost:8080/llp/4.jpg
改變默認的靜態(tài)資源路徑,比如希望在類路徑下增加 llpimg 目錄 作為靜態(tài)資源路徑 , 并完成測試.
(1)如圖所示
(2)配置src\main\resources\application.yml
spring:
mvc:
static-path-pattern: /llp/**
web:
resources:
#修改/指定 靜態(tài)資源的訪問路徑/位置
#
static-locations: ["classpath:/llpimg/","classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/"] #String[] staticLocations
(3)測試訪問http://localhost:8080/llp/5.png
(4)如果你配置 static-locations, 原來的訪問路徑就被覆蓋,如果需要保留,你再指定一下即可
spring:
mvc:
static-path-pattern: /llp/**
web:
resources:
#修改/指定 靜態(tài)資源的訪問路徑/位置
#
static-locations: ["classpath:/llpimg/","classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/"] #String[] staticLocations
到此這篇關(guān)于Springboot靜態(tài)資源的訪問方法介紹的文章就介紹到這了,更多相關(guān)Springboot靜態(tài)資源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談MyBatis-Plus學(xué)習(xí)之Oracle的主鍵Sequence設(shè)置的方法
這篇文章主要介紹了淺談MyBatis-Plus學(xué)習(xí)之Oracle的主鍵Sequence設(shè)置的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08詳解springboot設(shè)置默認參數(shù)Springboot.setDefaultProperties(map)不生效解決
這篇文章主要介紹了詳解springboot設(shè)置默認參數(shù)Springboot.setDefaultProperties(map)不生效解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Java模擬死鎖發(fā)生之演繹哲學(xué)家進餐問題案例詳解
這篇文章主要介紹了Java模擬死鎖發(fā)生之演繹哲學(xué)家進餐問題,結(jié)合具體演繹哲學(xué)家進餐問題的案例形式詳細分析了死鎖機制與原理,需要的朋友可以參考下2019-10-10