• Spring boot 静态资源访问


    Spring Boot 默认静态资源访问

    spring-configuration-metadata.json
    {
          "name": "spring.resources.static-locations",
          "type": "java.lang.String[]",
          "description": "Locations of static resources. Defaults to classpath:[/META-INF/resources/, /resources/, /static/, /public/].",
          "sourceType": "org.springframework.boot.autoconfigure.web.ResourceProperties",
          "defaultValue": [
            "classpath:/META-INF/resources/",
            "classpath:/resources/",
            "classpath:/static/",
            "classpath:/public/"
          ]
        }
    

    109e5a304e0089318c5398f00be1e412.png

    自定义静态资源地址

    例子1

    请求路径:/image/**
    请求文件:classpath:/images/

    @Configurationpublic class ImageMvcConfig extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/image/**")
                    .addResourceLocations("classpath:/images/");
        }
    }
    
    例子2

    请求路径:/resources/**
    请求文件:/public, classpath:/static/

    @Configuration@EnableWebMvcpublic class WebConfig implements WebMvcConfigurer {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/**")
                .addResourceLocations("/public", "classpath:/static/")
                .setCachePeriod(31556926);
        }
    }
    
    例子3
    spring:
      mvc:
        static-path-pattern: /image/**
      resources:
        static-locations: classpath:/images/
  • 相关阅读:
    《javascript设计模式》2接口
    对css类名className的一些操作的函数
    js设计模式方法的链式调用及回调
    js设计模式封装
    ajax的封装
    js设计模式单体(Singleton)
    js设计模式继承
    metasploit
    使用 AsyncCallback 处理异步调用
    log4net 的使用
  • 原文地址:https://www.cnblogs.com/yang21/p/11918097.html
Copyright © 2020-2023  润新知