Spring Boot 默认会挨个从

META/resources > resources > static > public 里面找是否存在相应的资源,如果有则直接返回。

默认配置:

spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

建议将静态资源和代码分离,将静态资源文件存储在CDN

application.properties

spring.resources.static-locations=classpath:/templates/,classpath:/static/,classpath:/public/

addResourceHandler方法是设置访问路径前缀,addResourceLocations方法设置资源路径,如果你想指定外部的目录也很简单,直接addResourceLocations指定即可,代码如下:

String outside_static = "/opt/app/outside_static";
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/templates/")
.addResourceLocations("classpath:/static/")
.addResourceLocations("classpath:/public/")
.addResourceLocations("file:" + outside_static);
@Configuration
public class StaticFilePathConfig implements WebMvcConfigurer {////implements WebMvcConfigurer //extends WebMvcConfigurationSupport // @Value("${file.staticAccessPath}")
// private String staticAccessPath;
// @Value("${file.uploadFolder}")
// private String uploadFolder; @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//staticAccessPath:/static/**
//"file:" + uploadFolder:/media/peter/Data/dev/bisonSpace/bison-web-service/bison-service-product/target/static/
//如果是Windows环境的话 file:=改为=》file:///
// registry.addResourceHandler(staticAccessPath).addResourceLocations("file:" + uploadFolder); File path = null;
try {
path = new File(ResourceUtils.getURL("classpath:").getPath());
//file:/data/github/testmanagement/target/testmanagement-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!
System.out.println(path.getPath());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//server.tomcat.basedir=outsidefile/jacococoverage
String outside_templates=path.getParentFile().getParentFile().getParent()+File.separator;
outside_templates=outside_templates.substring(5,outside_templates.length()); //String outside_static=path.getParentFile().getParentFile().getParent()+File.separator+"outsidefile"+File.separator+"jacococoverage"+File.separator; System.out.println(outside_static);
//file:/data/github/testmanagement/target/
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/templates/")
.addResourceLocations("classpath:/static/")
.addResourceLocations("classpath:/public/")
.addResourceLocations("file:" + outside_static);
// super.addResourceHandlers(registry);
}
}

SpringBoot2.X 静态文件配置的更多相关文章

  1. django静态文件配置

    开发环境配置 需要下面几个步骤 1. 在app目录下创建static目录,将静态文件和相关文件夹放到此目录下,如your_app/static/img等 2. 确保settings.py中的INSTA ...

  2. apache静态文件配置

    开发环境配置 需要下面几个步骤 1. 在app目录下创建static目录,将静态文件和相关文件夹放到此目录下,如your_app/static/img等 2. 确保settings.py中的INSTA ...

  3. Django(五)框架之模板继承和静态文件配置

    https://www.cnblogs.com/haiyan123/p/7731959.html 一.模板继承 目的是:减少代码的冗余 语法: {% block classinfo %} {% end ...

  4. Django框架之模板继承和静态文件配置

    一.模板继承 目的是:减少代码的冗余 语法: {% block classinfo %} {% endblock %} 具体步骤: 1.创建一个base.html文件,2.把要显示的页面的内容写在这里 ...

  5. Django框架----模板继承和静态文件配置

    母板 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...

  6. 55 Django静态文件配置

    一.Django静态文件配置 1.项目文件夹,新建一个文件夹statics 文件夹 2.在配置文件settings.py中,配置: 文件中有第句: STATIC_URL = '/static/'#静态 ...

  7. 第三百八十四节,Django+Xadmin打造上线标准的在线教育平台—路由映射与静态文件配置以及会员注册

    第三百八十四节,Django+Xadmin打造上线标准的在线教育平台—路由映射与静态文件配置以及会员注册 基于类的路由映射 from django.conf.urls import url, incl ...

  8. nginx访问静态文件配置

    通过nginx访问静态文件配置,均是在server模块中配置,有两种方式: 1.alias 通过alias关键字,重定义路径,如 server{     listen 7001;     server ...

  9. Django-模板继承、包含和静态文件配置

    一.模板继承 模板继承可以减少页面内容的重复定义,实现页面内容的重用 典型应用:网站的头部.尾部是一样的,这些内容可以定义在父模板中,子模板不需要重复定义 block标签:在父模板中预留区域,在子模板 ...

随机推荐

  1. 解决vs code编写python输出中文乱码问题

    环境: win7 vscode 1.37.0 插件: python Code Runner 乱码问题: 创建一个python文件,运行如下代码,打印显示乱码.: 解决乱码步骤: Ctrl+Shift+ ...

  2. Java虚拟机-------垃圾回收机机制

    概述 jvm中的堆图 在了解 垃圾回收器 之前,首先得了解一下垃圾回收器的几个名词. 1. 吞吐量CPU 用于运行用户代码的时间与 CPU 总消耗时间的比值.比如说虚拟机总运行了 100 分钟,用户代 ...

  3. javascript修改html <b>标签里面的内容

    简单实现仅供参考: javascript修改html <b>标签里面的化妆步骤内容<体><b style=“width:100px:height:100px:border ...

  4. Angular 开发环境搭建

    Angular 是一款开源 JavaScript 框架,由Google 维护,用来协助单一页面应用程序运行的.它的目标是增强基于浏览器的应用,使开发和测试变得更加容易.目前最新的 Angular 版本 ...

  5. Dreamweaver CS6 破解安装

    安装 双击Dreamweaver.dmg文件,然后Command+N,新建一个Finder,接着将Adobe Dreamweaver CS6拖到新建Finder的应用程序中.   在Finder中应用 ...

  6. ASE19 团队项目 模型组 scrum report集合

    scrum report 链接 scrum1 report scrum2 report scrum3 report scrum4 report scrum5 report scrum6 report ...

  7. Easy UI combobox实现类似 Select2的效果,下拉带搜索框

    一直在开发一个新系统,其中用Easy UI作为前端框架,少不了用 combobox做为一个 下拉控件,它支持 可编辑 模糊本地数据过滤,也可支持 不可编辑 下拉 选择的功能: $('#ID' ).co ...

  8. 使用Django开发简单接口:文章增删改查

    目录 1.一些准备工作 安装django 创建django项目 创建博客应用(app) 2.models.py 3.django admin 登录 创建超级用户 4.修改urls.py 5.新增文章接 ...

  9. pandas中Series对象下的str所拥有的方法(df["xx"].str)

    在使用pandas的时候,经常要对DataFrame的某一列进行操作,一般都会使用df["xx"].str下的方法,但是都有哪些方法呢?我们下面来罗列并演示一下.既然是df[&qu ...

  10. 8.3.ZooKeeper集群安装配置

    1.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式. 单机模式:Zookeeper只运行在一台服务器上,适合测试环境: 伪集群模式:就是在一台物理机上 ...