通过idea快速搭建一个springboot项目:

springboot版本2.1.6

在网上看的资料,springboot静态资源访问如下:

"classpath:/META‐INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径

之后在测试访问静态资源的时候没有问题,通过代码配置的方式访问登录页面资源的时候出现了404。

在网上找了很多关于静态资源访问404的问题,貌似不是在application.yml(或properties)中配置访问路径,就是自己定义config类。

后来翻看源码发现,由于在代码配置的时候。继承了WebMvcConfigurationSupport这个类,导致整个资源访问失效,具体原因如下:

第一步:

  确认springboot静态资源访问的位置,由于springboot自动加载配置,所以找到WebMvcAutoConfiguration这个配置类,里面有很多方法,

  只需找到资源映射的方法:

public void addResourceHandlers(ResourceHandlerRegistry registry) {
  if (!this.resourceProperties.isAddMappings()) {
    logger.debug("Default resource handling disabled");
  } else {
    Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
    CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
    <!-- /webjars/** 访问的,资源映射规则-->
    if (!registry.hasMappingForPattern("/webjars/**")) {
      this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).
          addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
    }
     <!-- 静态资源映射规则-->
    String staticPathPattern = this.mvcProperties.getStaticPathPattern();
    if (!registry.hasMappingForPattern(staticPathPattern)) {
      this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).
          addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).
                    setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
     }
   }
}

 第一种webjars加载:所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;

 第二种静态资源映射:

  staticPathPattern  点击查看源码:staticPathPattern = /**;

  resourceProperties.getStaticLocations() 点击查看源码:

 private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
private String[] staticLocations;
private boolean addMappings;
private final ResourceProperties.Chain chain;
private final ResourceProperties.Cache cache; public ResourceProperties() {
this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
this.addMappings = true;
this.chain = new ResourceProperties.Chain();
this.cache = new ResourceProperties.Cache();
} public String[] getStaticLocations() {
return this.staticLocations;
}

通过以上源码看出,访问资源路径跟一开始测试时的一样,没问题,为何通过配置设置登录页面就无法访问静态资源了呢,反复查看WebMvcAutoConfiguration源码发现如下:

@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {=
@ConditionalOnClass 和 @ConditionalOnMissingBean 这两个注解的条件成立WebMvcAutoConfiguration才会自动加载,
在配置登录页的时候,正好继承了WebMvcConfigurationSupport类 @ConditionalOnMissingBean({WebMvcConfigurationSupport.class}) 容器没有加载这个bean时自动加载WebMvcAutoConfiguration,
继承WebMvcAutoConfiguration的方式也可以,不过需要手动添加资源映射路径,WebMvcAutoConfiguration中的其他自动加载项也需要自己处理。 @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class}) 容器加载这个bean时自动加载WebMvcAutoConfiguration
重新修改配置类 实现WebMvcConfigurer接口对登录页进行处理,静态资源访问正常。

springboot 2.X 在访问静态资源的的时候出现404的问题的更多相关文章

  1. SpringBoot学习5:访问静态资源

    springboot默认从项目的resources里面的static目录下或者webapp目录下访问静态资源 方式一:在resources下新建static文件(文件名必须是static) 在浏览器中 ...

  2. springboot(二 如何访问静态资源和使用模板引擎,以及 全局异常捕获)

    在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: /static / ...

  3. Springboot访问静态资源&WebJars&图标&欢迎页面

    目录 概述 1.访问WebJar资源 2.访问静态资源 3.favicon.ico图标 4.欢迎页面 概述 使用Springboot进行web开发时,boot底层实际用的就是springmvc,项目中 ...

  4. springBoot怎样访问静态资源?+静态资源简介

    1.静态资源 怎样通过浏览器访问静态资源? 注意:不需要加static目录.因为只是告诉springboot目录,而不是静态资源路劲. 这时访问路径就需要加上/static

  5. SpringBoot: 5.访问静态资源(转)

    springboot默认从项目的resources里面的static目录下或者webapp目录下访问静态资源 方式一:在resources下新建static文件(文件名必须是static) 在浏览器中 ...

  6. 解决SpringBoot页面跳转无法访问静态资源的问题

    初学SpringBoot,写项目的时候遇到了问题,原本的页面是这样的 但启动项目后是这样的 这是因为thymeleaf中引入静态资源及模板需要使用到 th:xxx 属性,否则无法在动态资源中访问静态资 ...

  7. 【SpringBoot】06.SpringBoot访问静态资源

    SpringBoot访问静态资源 1.SpringBoot从classpath/static的目录 目录名称必须是static 启动项目,访问http://localhost:8080/0101.jp ...

  8. springboot无法访问静态资源

    无法访问static下的静态资源 1.在application.yml中添加 resources: static-locations: classpath:/META-INF/resources/,c ...

  9. springboot+themeleaf+bootstrap访问静态资源/无法访问静态资源/图片

    在网页HTML上访问静态资源的正确写法例: 1.<img src="../../static/bootstarp/img/2.jpg"     th:src="@{ ...

随机推荐

  1. Store-exclusive instruction conflict resolution

    A data processing system includes a plurality of transaction masters (4, 6, 8, 10) each with an asso ...

  2. 【每日一句】make a scene

    scene有场面.场景的意思,只是make a scene可不是指做场面.scene还有还有一个意思,指很生气而大吵大闹(的情景),因此make a scene就是指某人情绪失控发脾气.如今一起从以下 ...

  3. QWidget居中显示(qt窗口坐标原点是在”左上角”的,有图)

    转载请说明出处, 并附上原文链接http://blog.csdn.net/qq907482638/article/details/72189014. 问题描述 在Qt学习过程中,在让QDialog居中 ...

  4. hdu - 4971 - A simple brute force problem.(最大权闭合图)

    题意:n(n <= 20)个项目,m(m <= 50)个技术问题,做完一个项目能够有收益profit (<= 1000),做完一个项目必须解决对应的技术问题,解决一个技术问题须要付出 ...

  5. 微信公众平台通用接口API指南

    微信公众平台 通用接口 消息接口 开发模式 作者:方倍工作室原文:http://www.doucube.com/index.php?m=Article&a=show&id=5 微信公众 ...

  6. UVA - 10312 Expression Bracketing

    Description Problem A Expression Bracketing Input: standard input Output: standard output Time Limit ...

  7. 用 theano 求解 Logistic Regression (SGD 优化算法)

    1. model 这里待求解的是一个 binary logistic regression,它是一个分类模型,参数是权值矩阵 W 和偏置向量 b.该模型所要估计的是概率 P(Y=1|x),简记为 p, ...

  8. FastDFS是纯C语言实现,只支持Linux,适合以中小文件为载体的在线服务,还可以冗余备份和负载均衡

    一.理论基础 FastDFS比较适合以中小文件为载体的在线服务,比如跟NGINX(APACHE)配合搭建图片服务器. 分布式文件系统FastDFS FastDFS是纯C语言实现,只支持Linux.Fr ...

  9. android studio中使用9-patch报错mergeDebugResource及Duplicate resources错误处理

    由于项目中新导入了两张图片,进行9-patch之后,文件名称包含XXXX.9.png , 而android studio 对资源文件的名称有要求仅支持[A-Z][a-z][0-9]格式  而XXX.9 ...

  10. JDK源码阅读—ArrayList的实现

    1 继承结构图 ArrayList继承AbstractList,实现了List接口 2 构造函数 transient Object[] elementData; // 数组保存元素 private i ...