最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解讀靜態(tài)資源訪問static-locations和static-path-pattern

 更新時間:2025年01月30日 07:51:09   作者:學習中的小亮  
本文主要介紹了Spring Boot中靜態(tài)資源的配置和訪問方式,包括靜態(tài)資源的默認前綴、默認地址、目錄結(jié)構(gòu)、訪問路徑以及靜態(tài)資源處理器的工作原理,通過配置文件和實現(xiàn)`WebMvcConfigurer`接口,可以自定義靜態(tài)資源目錄和訪問前綴

靜態(tài)資源訪問static-locations和static-path-pattern

靜態(tài)資源配置底層源碼

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		if (!this.resourceProperties.isAddMappings()) {
			logger.debug("Default resource handling disabled");
				return;
		}
        //配置訪問地址為/webjars/**時,去/META-INF/resources/webjars文件夾下尋找資源
		addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
		addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
				            
        registration.addResourceLocations(this.resourceProperties.getStaticLocations());
		if (this.servletContext != null) {
				ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);
				registration.addResourceLocations(resource);
		}
			});
		}

靜態(tài)資源默認前綴:

private String staticPathPattern = "/**";

靜態(tài)資源默認地址:

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
				"classpath:/resources/", "classpath:/static/", "classpath:/public/" };

		/**
		 * Locations of static resources. Defaults to classpath:[/META-INF/resources/,
		 * /resources/, /static/, /public/].
		 */
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;

靜態(tài)資源目錄

如果每個目錄下面都有相同的文件,那么訪問的優(yōu)先級為 META-INF>resources>static>public

靜態(tài)資源訪問前綴(默認無前綴)可以使用下面的yaml內(nèi)容來設(shè)置

spring:
  mvc:
    static-path-pattern: /liang/**  //會導致歡迎頁和favicon.ico失效

靜態(tài)資源存放地址(靜態(tài)文件只能存放在文件夾yuan里面)

spring:
  web:
    resources:
      static-locations: classpath:/yuan/

當配置文件如下

spring
  web:
    resources:
      static-locations: classpath:/yuan/
  mvc:
    static-path-pattern: /liang/**

可以直接通過地址 http://localhost:8080/liang/a.png 直接進行訪問,查看到想要結(jié)果

當靜態(tài)訪問前綴為/**時,靜態(tài)資源目錄下有一個a.png,controller控制層的@RequestMapping("/a.png")。

得到結(jié)果

結(jié)論:請求進來,先去controller看能不能處理,不能處理的所有請求又都交給靜態(tài)資源處理器。靜態(tài)資源找不到就報404

為什么歡迎頁(index.html)有靜態(tài)資源訪問前綴就不能訪問了

通過 http://localhost:8080/liang/index.html可以直接訪問到界面,但是通過 http://localhost:8080/liang 或者 http://localhost:8080/ 都不能直接訪問到index。

但是如果把靜態(tài)資源訪問前綴去除,就可以通過 http://localhost:8080/ 訪問到index.html了.

這是因為底層做了處理

實現(xiàn)WebMvcConfigurer接口

會把自定義配置加載到默認的配置中

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //registry.addResourceHandler("訪問的路徑").addResourceLocations("資源的路徑");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
    }

配置文件中靜態(tài)資源目錄為

可以簡單理解為:實現(xiàn)WebMvcConfigurer接口,可以把自己自定義的一些配置加載到系統(tǒng)的默認配置中

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

邯郸县| 水富县| 太仓市| 东丰县| 苍梧县| 林西县| 万全县| 方正县| 荣成市| 项城市| 静安区| 宜兰市| 县级市| 会理县| 盐亭县| 叶城县| 天长市| 调兵山市| 霍林郭勒市| 鄂托克旗| 五指山市| 平和县| 嵩明县| 永修县| 小金县| 阿拉善右旗| 宁河县| 洪泽县| 海宁市| 荥阳市| 瑞金市| 夏津县| 措美县| 旅游| 建德市| 钟山县| 弥渡县| 连城县| 石台县| 双城市| 碌曲县|