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

SpringBoot中的static靜態(tài)資源訪問、參數(shù)配置、代碼自定義訪問規(guī)則詳解

 更新時間:2023年07月31日 14:20:06   作者:Bulut0907  
這篇文章主要介紹了SpringBoot的static靜態(tài)資源訪問、參數(shù)配置、代碼自定義訪問規(guī)則,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1. 靜態(tài)資源

查看源碼如下:

package org.springframework.boot.autoconfigure.web.servlet;
......省略部分......
public class WebMvcAutoConfiguration {
......省略部分......
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                // 將靜態(tài)資源映射,都添加到映射中心
                this.addResourceHandler(registry, this.mvcProperties.getWebjarsPathPattern(), "classpath:/META-INF/resources/webjars/");
                this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
                    registration.addResourceLocations(this.resourceProperties.getStaticLocations());
                    if (this.servletContext != null) {
                        ServletContextResource resource = new ServletContextResource(this.servletContext, "/");
                        registration.addResourceLocations(new Resource[]{resource});
                    }
                });
            }
        }
......省略部分......
        private void addResourceHandler(ResourceHandlerRegistry registry, String pattern, Consumer<ResourceHandlerRegistration> customizer) {
            if (!registry.hasMappingForPattern(pattern)) {
                ResourceHandlerRegistration registration = registry.addResourceHandler(new String[]{pattern});
                customizer.accept(registration);
                registration.setCachePeriod(this.getSeconds(this.resourceProperties.getCache().getPeriod()));
                registration.setCacheControl(this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl());
                registration.setUseLastModified(this.resourceProperties.getCache().isUseLastModified());
                this.customizeResourceHandlerRegistration(registration);
            }
        }
......省略部分......
}

1.1 默認靜態(tài)資源

默認的靜態(tài)資源目錄為:resources/META-INF/resources、resources/public、resources/resources、resources/static。一般用于存放圖片、視頻、css、js文件

默認的訪問靜態(tài)資源的URL根路徑為:/**

所以訪問resources/META-INF/resources/baidu1.jpg的URL為http://localhost:8080/baidu1.jpg

1.2 Controller高優(yōu)先級

如果一個Controller的的@RequestMapping定義的訪問路徑,和靜態(tài)資源的URL訪問路徑一樣,則返回Controller的內(nèi)容

1.3 修改靜態(tài)資源的URL根路徑

可以通過配置參數(shù): spring.mvc.static-path-pattern: /static/**,修改靜態(tài)資源的URL根路徑。此時訪問resources/META-INF/resources/baidu1.jpg的URL為http://localhost:8080/static/baidu1.jpg

1.4 修改靜態(tài)資源的目錄

可以通過配置參數(shù):spring.web.resources.static-locations: [classpath:/my-resources/],修改靜態(tài)資源的目錄。會使resources/public、resources/resources、resources/static目錄失效,但resources/META-INF/resources目錄不失效

1.5 訪問webjars依賴包的靜態(tài)資源

會自動將resources/META-INF/resources/webjars/**,作為靜態(tài)資源目錄

這里我們以jquery為例,進行講解。在pom.xml添加如下依賴

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.6.1</version>
        </dependency>

org.webjars.jquery的jar包的目錄結(jié)構(gòu)如下:

jquery的jar包的目錄結(jié)構(gòu)

然后訪問http://localhost:8080/static/webjars/jquery/3.6.1/jquery.js。顯示如下:

jquery效果

可以使用spring.mvc.webjars-path-pattern=/wj/**修改webjars路徑訪問前綴,這樣需要通過http://localhost:8080/static/wj/jquery/3.6.1/jquery.js進行訪問

1.6 靜態(tài)資源的關(guān)閉

可以通過配置參數(shù):spring.web.resources.add-mappings=false,關(guān)閉所有靜態(tài)資源

1.7 靜態(tài)資源在瀏覽器的緩存

  • 瀏覽器會將靜態(tài)資源緩存,在緩存時間過期前,不會向服務(wù)器進行靜態(tài)資源的請求。通過配置參數(shù)進行設(shè)置:spring.web.resources.cache.period=3,單位為秒,默認為0秒
  • cacheControl: HTTP緩存控制。參考: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Caching。通過spring.web.resources.cache.cachecontrol.max-age=7200設(shè)置,默認是7200秒,優(yōu)先級比spring.web.resources.cache.period高。如果沒手動設(shè)置該參數(shù),但手動設(shè)置了spring.web.resources.cache.period,則period參數(shù)生效
  • useLastModified:是否使用最后一次修改。配合cacheControl使用。比如如果瀏覽器訪問了一個靜態(tài)資源index.js,如果服務(wù)器這個資源沒有發(fā)生變化,下次訪問的時候就可以直接讓瀏覽器用自己緩存中的東西,而不用給服務(wù)器發(fā)請求,此時瀏覽器狀態(tài)碼為304。通過spring.web.resources.cache.use-last-modified=true設(shè)置,默認為true
  • 共享緩存和私有緩存。共享緩存: 客戶瀏覽器和代理服務(wù)器都會緩存;私有緩存: 只有客戶瀏覽器會緩存。通過spring.web.resources.cache.cachecontrol.cache-public=truespring.web.resources.cache.cachecontrol.cache-private=false設(shè)置。默認由瀏覽器自行決定,但一般是私有緩存

1.8 靜態(tài)資源實戰(zhàn)

Controller中定義了一個@RequestMapping(“/static/baidu1.jpg”)路徑,和resources\META-INF\resources\baidu1.jpg靜態(tài)資源的路徑一樣。內(nèi)容如下:

package com.hh.springboottest.myController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
    @RequestMapping("/static/baidu1.jpg")
    public String helloHandle() {
        return "hello springboot";
    }
}

resources目錄內(nèi)容如下。分別有resources/META-INF/baidu1.jpg、resources/my-resources/baidu2.jpg、resources/public/baidu3.jpg、resources/resources/baidu4.jpg、resources/static/baidu5.jpg

resources目錄內(nèi)容

application.yaml內(nèi)容如下。分別定義了靜態(tài)資源訪問URL根路徑,和靜態(tài)資源目錄

spring:
  mvc:
    static-path-pattern: /static/**
  web:
    resources:
      static-locations: [classpath:/my-resources/]

訪問http://localhost:8080/static/baidu1.jpg,效果如下:

訪問static/baidu1.jpg

訪問http://localhost:8080/static/baidu2.jpg,效果如下:

訪問static/baidu2.jpg

http://localhost:8080/static/baidu3.jpg、http://localhost:8080/static/baidu4.jpg、http://localhost:8080/static/baidu5.jpg不能訪問

1.9 通過代碼自定義靜態(tài)資源訪問規(guī)則

如下。添加一個靜態(tài)資源訪問URL,并指定其訪問資源路徑和緩存時間。其原理是給容器中添加一個WebMvcConfigurer組件,讓配置的底層行為都生效

import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.concurrent.TimeUnit;
// @EnableWebMvc  // 禁用springBoot的默認配置
// 這是一個配置類。給容器中放一個WebMvcConfigurer組件,就能自定義底層。有以下兩種方式
//     1. 可以讓配置類實現(xiàn)WebMvcConfigurer接口
//     2. 可以在配置類中,將@Bean注解標注在方法上,然后方法返回WebMvcConfigurer
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 默認就會保留以前規(guī)則
        // WebMvcConfigurer.super.addResourceHandlers(registry);
        // 自己添加新的規(guī)則
        registry.addResourceHandler("/my-static/**")
                .addResourceLocations("classpath:/static1/", "classpath:/static2/")
                .setCacheControl(CacheControl.maxAge(1180, TimeUnit.SECONDS));
    }
}

到此這篇關(guān)于SpringBoot的static靜態(tài)資源訪問、參數(shù)配置、代碼自定義訪問規(guī)則的文章就介紹到這了,更多相關(guān)SpringBoot static靜態(tài)資源訪問內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

开原市| 罗源县| 陆丰市| 霞浦县| 临武县| 策勒县| 清水县| 平泉县| 迁西县| 军事| 宣化县| 抚顺县| 图木舒克市| 宁强县| 玛纳斯县| 区。| 津南区| 长顺县| 碌曲县| 迭部县| 新余市| 玛纳斯县| 河南省| 屏南县| 阿拉尔市| 乡城县| 江永县| 沙湾县| 上杭县| 兴隆县| 永修县| 岐山县| 卢氏县| 甘泉县| 江陵县| 会泽县| 台东县| 林甸县| 邓州市| 郧西县| 柳江县|