设置spring.web.resources.add-mappings=false,界面无法访问何解?

506 阅读1分钟

我们先来看看spring.web.resources.add-mappings这个属性 spring文档中给出的是

image.png 意思是ResourceHandler不会自动地添加,具体是如何影响地,可看看这个链接中的文章,我觉得讲得很清楚www.bmabk.com/index.php/p… 那么现在我把它设置为false了,ResourceHandler不会自己加载,当我访问项目主页http://localhost:9000/index.html 时,springboot找不到index.html的静态资源,所以会出现错误页面 错误信息提示No handler found for GET /index.html既然它不能自动加,那就自己手动加上去: 创建一个实现了WebMvcConfigurer接口的类,重写addResourceHandlers()方法即可, 代码如下:

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

这里需要注意的是所有的静态资源都要加上,否则可能出现页面显示不完全的问题。