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)文章
java中Hashtable和HashMap的區(qū)別分析
java中Hashtable和HashMap的區(qū)別分析,需要的朋友可以參考一下2013-04-04
SpringBoot整合ShedLock解決定時任務(wù)防止重復(fù)執(zhí)行的問題
ShedLock是一個用于分布式系統(tǒng)中防止定時任務(wù)重復(fù)執(zhí)行的庫,本文主要介紹了SpringBoot整合ShedLock解決定時任務(wù)防止重復(fù)執(zhí)行的問題,具有一定的參考價值,感興趣的可以了解一下2025-01-01
java快速解析路徑中的參數(shù)(&與=拼接的參數(shù))
這篇文章主要介紹了java快速解析路徑中的參數(shù)(&與=拼接的參數(shù)),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-02-02

