Springboot静态资源路径配置访问无效处理方法

一次踩坑记录,静态资源配置迟迟出不来。后来网上查了下,是有两个地方是需要配置的

1. application-xxx.yml配置文件

spring:
  mvc:
    static-path-pattern: /**
  resources:
    static-locations: classpath:/static/

2.新建一个mvc的配置文件  

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

/**
 * @author Howe
 * @description:
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //将所有/static/** 访问都映射到classpath:/static/ 目录下
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}

  然后配置文件需要放在resources目录下的static文件夹里

posted @ 2021-06-12 14:07  howe_ya  阅读(2515)  评论(0)    收藏  举报