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路径

<link rel="Stylesheet" media="screen" href="/static/editor-app/libs/ng-grid-2.0.7.min.css" type="text/css"/>
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值