Spring Boot无法访问静态资源css、js、图片等

 1、static是默认静态资源目录,建好项目默认是不需要配置,也能访问的。如果访问不了,很可能是因为启动类加入注解了@EnableWebMvc,删了。

2、如果一定要使用@EnableWebMvc,加入下面的配置类即可,以代码方式指定视图相关配置,包括静态资源访问路径:

 

@Configuration
public class MyWebMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //配置静态资源路径
        registry.addResourceHandler("/**")
                .addResourceLocations("resources/", "static/", "public/",
                        "META-INF/resources/")
                .addResourceLocations("classpath:resources/", "classpath:static/",
                        "classpath:public/", "classpath:META-INF/resources/");

    }
}

 

你可能感兴趣的:(SSM,spring,boot,java)