spring boot 2.0.1(十二)找不到静态资源文件夹static目录下文件,被DispatcherServlet拦截

spring boot 2.0.1找不到静态资源文件夹static目录下文件,被DispatcherServlet ,拦截,报错如下:

[2018-07-10 15:04:43,254] [qtp5596963-75] [WARN ] org.springframework.web.servlet.PageNotFound 1205 -- No mapping found for HTTP request with URI [/static/js/jquery.min.js] in DispatcherServlet with name 'dispatcherServlet'
[2018-07-10 15:04:43,263] [qtp5596963-30] [WARN ] org.springframework.web.servlet.PageNotFound 1205 -- No mapping found for HTTP request with URI [/static/bootstrap-4.0.0-dist/js/bootstrap.bundle.js] in DispatcherServlet with name 'dispatcherServlet'
[2018-07-10 15:04:43,279] [qtp5596963-75] [WARN ] org.springframework.web.servlet.PageNotFound 1205 -- No mapping found for HTTP request with URI [/static/bootstrap-4.0.0-dist/js/bootstrap.js] in DispatcherServlet with name 'dispatcherServlet'

官方文档给出

27.1.5 Static Content
By default, Spring Boot serves static content from a directory called /static (or > /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext. It uses the ResourceHttpRequestHandler from Spring MVC so that you can modify that behavior by adding your own WebMvcConfigurer and overriding the addResourceHandlers method.

spring boot默认静态资源的路径是/static ,也就是说不用配置也可以访问静态资源文件,引入activity工作流后,直接访问static下的静态资源就访问不了了。些时需要实现WebMvcConfigurer重写addResourceHandlers方法。添加StaticResourceConfig类如下:

@Configuration
@Slf4j
@EnableWebMvc
public class StaticResourceConfig implements WebMvcConfigurer {

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

注意需要添加@EnableWebMvc注解。
引用js、css文件,前面需要带上/static路径


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