? ? ?? ?做web開發(fā)的時候,我們往往會有很多靜態(tài)資源,如html、圖片、css等。那如何向前端返回靜態(tài)資源呢?
? ? ? ?以前做過web開發(fā)的同學(xué)應(yīng)該知道,我們以前創(chuàng)建的web工程下面會有一個webapp的目錄,我們只要把靜態(tài)資源放在該目錄下就可以直接訪問。
? ? ? ?但是,基于Spring boot的工程并沒有這個目錄,那我們應(yīng)該怎么處理?
? ? ? ?我們通過最原始的方法和springboot中的方法分別進行說明。
1、原始方式
? ? ? ? 我們首先來分享一種最笨的辦法,就是將靜態(tài)資源通過流的方式直接返回給前端,步驟如下:
01、我們在maven工程的resources的根目錄下建立一個html的目錄,然后我們把html文件放在該目錄下
2、并且規(guī)定任何訪問路徑以?/static/?開頭的才能訪問?/html?目錄下的靜態(tài)資源,其實現(xiàn)如下:
前端訪問的 Url 是:http://localhost:8080/static/login.html
前端訪問的 Url 中的 uri 是:/static/login.html
實際訪問的是類路徑下:newUrl = /xxxxxx/webapps/ROOT/WEB-INF/classes/html/login.html
使用流的方式寫出到客戶端:
- FileReader reader = new FileReader(new File(newUrl));
- response.getOutputStream().write(sb.toString().getBytes());?
@Controller
public class StaticResourceController {
? ?@RequestMapping("/static/**")
? ?public void getHtml(HttpServletRequest request, HttpServletResponse response) {
? ? ? ?String uri = request.getRequestURI(); // static/login.html
? ? ? ?String[] arr = uri.split("static/");
? ? ? ?String resourceName = "index.html"; // 默認值是訪問index.html
? ? ? ?if (arr.length > 1) {
? ? ? ? ? ?resourceName = arr[1]; // login.html
? ? ? ?}
//類路徑下:/xxxxxx/webapps/ROOT/WEB-INF/classes/html/login.html
? ? ? ?String url = StaticResourceController.class.getResource("/").getPath() + "html/" + resourceName;
? ? ? ?try {
? ? ? ? ? // 讀取類路徑下的靜態(tài)資源文件
FileReader reader = new FileReader(new File(url));
BufferedReader br = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
//使用流的方式寫出到客戶端
response.getOutputStream().write(sb.toString().getBytes());
response.flushBuffer();
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}
? ?}
}
? ? ?? ?其實現(xiàn)過程很簡單,就是先從路徑中分離出來資源uri,然后從static目錄下讀取文件,并輸出到前端。
? ? ?? ?因為只做簡單演示,所以這里只處理了文本類型的文件,圖片文件可以做類似的處理。當(dāng)然,我們在實際中肯定不會這么做,Spring Boot 也肯定有更好的解決辦法。
? ? ?? ?不過這個辦法雖然有點笨,但確是最本質(zhì)的東西,無論框架如何方便的幫我們處理了這類問題,但是拋開框架,我們依然要能夠熟練的寫出一個web項目,只有知道其實現(xiàn)原理,你才會在遇到問題時能得心應(yīng)手。
現(xiàn)在我們再來看看Spring boot對靜態(tài)資源的支持。
3、Spring boot默認靜態(tài)資源訪問方式
? ? ?? ?Spring boot默認訪問的就是 /** ,所以Spring boot訪問:當(dāng)前項目根路徑/ + 靜態(tài)資源文件名就會自動的找到對應(yīng)的文件。對應(yīng)的文件在 類路徑下默認的四個靜態(tài)資源文件目錄下,因此
? ? ?? ?Spring boot默認對/**的訪問 是可以直接訪問類路徑下的四個靜態(tài)資源目錄下的文件:
- classpath:/public/
- classpath:/resources/
- classpath:/static/
- classpath:/META-INFO/resouces/
我們現(xiàn)在就在資源文件resources目錄下建立如下四個目錄:
? ? ?? ?注意藍色條下的資源文件夾resources與類路徑下的文件夾classpath:/resources是不同的,藍色條下的resources代表的是該目錄下的文件為資源文件(即類路徑),在打包的時候會將該目錄下的文件全部打包的類路徑下,這個名稱是可以改的,在pom.xml指定資源目錄即可:
<resources>
?? ? <resource>
?? ? ? ? <directory>src/main/resources</directory>
?? ? </resource>
</resources>
? ? ?? ?而類路徑下的resources是spring boot默認的靜態(tài)資源文件夾之一,和public、static以及MEAT-INFO/resources的功能相同?,F(xiàn)在我們重啟Spring boot就可以通過:
- http://localhost:8080/1.html
- http://localhost:8080/2.html
- http://localhost:8080/3.html
- http://localhost:8080/4.html
4、Spring boot指定靜態(tài)資源訪問前綴
? ? ?? ?前面我們講過,如果使用springboot默認訪問/** 的方式,默認訪問的是類路徑下的resources、public、static及MEAT-INFO/resources四個默認的靜態(tài)資源目錄下的文件,訪問方式是:當(dāng)前項目根路徑/+靜態(tài)資源名,如:http://localhost:8080/login.html。但如果在Controller中映射的請求也是?/login.html?則如何處理?
?以代碼方式說明,代碼如下:
01、controller代碼
@RestController
public class HelloController {
@RequestMapping("/login.html")
public String login(Model model){
return "request mapping login.html";
}
}
02、login.html 代碼?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1> This is login.html page content </h1>
</body>
</html>
03、主啟動類代碼
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
03、啟動Springboot主啟動類,訪問?http://localhost:8080/login.html?測試結(jié)果如下:
? ? ?? ?我們發(fā)現(xiàn),請求進來,先去找Controller看能不能處理。不能處理的所有請求又都交給靜態(tài)資源處理器。靜態(tài)資源可以找到,則返回靜態(tài)資源文件,如果靜態(tài)資源也找到返回404。原因是默認訪問的是/**,springboot的一些攔截器會對 類路徑下的所有資源進行攔截,從而導(dǎo)致直接訪問controller。
? ? ?? ?那么Springboot中是如何解決上述問題呢?
? ? ?? ?可以在springboot配置文件中進行指定訪問靜態(tài)資源的前綴例如:/res代表前綴,從而可以跟根據(jù)前綴映射到對應(yīng)的靜態(tài)資源,底層實現(xiàn)的原理和之前的原始方式相同。因此 在指定訪問靜態(tài)資源的前綴后,訪問靜態(tài)資源是:當(dāng)前項目+/res/ +靜態(tài)資源文件名 =?在靜態(tài)資源文件夾下查找靜態(tài)資源文件。
04.在application.properties/yml配置文件中使用 spring.mvc.static-path-pattern=/res/**
spring.mvc.static-path-pattern=/res/**
05、啟動主啟動類,分別訪問?
- http://localhost:8080/res/login.html
- http://localhost:8080/login.html
06、運行結(jié)果分別如下:?
5、自定義靜態(tài)資源目錄
? ? ?? ??springboot除了可以指定靜態(tài)資源訪問路徑的前綴情況下,還可以不使用springboot的默認靜態(tài)資源目錄resources、public、static及MEAT-INFO/resources,可以使用spring.web.resources.static-locations自定義指定類路徑下資源訪問目錄,但是指定后類路徑下的默認的靜態(tài)資源路徑則失效。因為這個配置會覆蓋Spring boot默認的靜態(tài)資源目錄,例如如果按示例中配置,則無法再訪問static、public、resources等目錄下的資源了。
以代碼方式說明:
01、在application.properties/yml配置文件中添加配置spring.web.resources.static-locations
spring.mvc.static-path-pattern=/res/**
spring.web.resources.static-locations= classpath:/demodir/
02、啟動主啟動類,分別訪問:
- http://localhost:8080/res/demo.html
- http://localhost:8080/res/login.html
03、除了在配置文件中配置外,也可以通過代碼配置(了解)
? ? ?? ?我們現(xiàn)在就來自定義一個靜態(tài)資源目錄,我們定義一個images的目錄來存放圖片,所有/image/**的路徑都會訪問images目錄下的資源:
@Configuration
public class PublicMvcConfig extends WebMvcConfigurerAdapter {
? ?@Override
? ?public void addResourceHandlers(ResourceHandlerRegistry registry) {
? ? ? ?registry.addResourceHandler("/public/**")
? ? ? ? ? ? ? ?.addResourceLocations("classpath:/public/");
? ?}
}
?? ? ?? ?WebMvcConfigurerAdapter是Spring提供的一個配置mvc的適配器,里面有很多配置的方法,addResourceHandlers就是專門處理靜態(tài)資源的方法,其他方法后續(xù)我們還會講到。
6、Springboot靜態(tài)資源訪問總結(jié)。
? ? ? ?本文主要給大家分享了Spring boot 對靜態(tài)資源的處理方式,Spring boot 默認可以訪問:
classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
四個目錄下的靜態(tài)資源,我們也可以根據(jù)自己的需要進行個性化配置。
? ? ? ?最后,需要說明一點的是,如果這四個目錄中存在相同名稱的資源,那會優(yōu)先返回哪個目錄下的資源呢?
? ? ? ?大家通過static-locations的默認值順序應(yīng)該能猜到,默認情況下,Spring boot會優(yōu)先返回/META-INF/resources下的資源。
? ? ? ? 當(dāng)然,因為我們可以自定義static-locations的值,所以這個優(yōu)先順序也是可以調(diào)整的。
7、注意:訪問js、jq等靜態(tài)資源需方式。
01、方式一:直接加載static下的文件
- 將所引用的版本下載好
- 引入jar包
//使用thymeleaf
<script type="text/javascript" th:src="@{/js/jquery-3.3.1.min.js}"></script>
//一起放在static下
<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
02、方式二:.引入pom,自動映射 /webjars/**
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.5.1</version>
</dependency>
<!-- 引入jQuery-->
<script type="text/javascript" th:src="@{/webjars/jquery/3.5.1/jquery.min.js}"></script>
?03、方式三:在線引入
<script th:src="@{https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js}"></script>
8、靜態(tài)資源原理
? ? ? ? 找到springmvc的自動配置類webmvcAutoConfiguration自動配置類。
?
? ? ?資源處理的默認規(guī)則:
?文章來源:http://www.zghlxwxcb.cn/news/detail-402447.html
? ? ? ?讀取靜態(tài)資源目錄的順序:
?
11Springboot的默認配置文件和外部配置文件及加載順序
https://blog.csdn.net/qq_41946216/article/details/124816948?spm=1001.2014.3001.5501文章來源地址http://www.zghlxwxcb.cn/news/detail-402447.html
到了這里,關(guān)于10SpringBoot 靜態(tài)資源訪問 11Springboot的默認配置文件和外部配置文件及加載順序的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!