SpringBoot静态资源无法访问

Posted 胡乐天

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot静态资源无法访问相关的知识,希望对你有一定的参考价值。

主要用于静态资源放的目录不是static(本例中用的是:statics),因为默认的静态资源目录就是static,所以如果放到static目录下,大概率是不需要配置的吧,我的理解如此,若有不同见解,请评论哦!

方法一:使用配置文件(yml)

spring:
  mvc:
    static-path-pattern: /statics/**  #放开springboot对静态资源的拦截(判断路径符合这个规则的,为静态资源)
  resources:
    static-locations: classpath:/statics/ #静态资源寻找位置

方法二:使用配置类

package com.lt.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置静态资源url识别和静态资源位置
 */
@Configuration
public class WebConfig implements WebMvcConfigurer 

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) 
        registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
    


若有错,欢迎评论区讨论!!!

以上是关于SpringBoot静态资源无法访问的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot静态资源无法访问

springboot无法访问静态资源

SpringBoot无法访问引入静态资源,jscssimg等,控制台爆红404

springboot打包后静态资源webapp文件夹无法打包进去

SpringBoot找不到静态资源文件

不登录就无法访问静态资源目录(我使用的是spring security)