Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

Spring Boot中为配置虚拟路径,重写WebMvcConfigurer类的addResourceHandlers方法不生效

在前后端分离项目中,为了解决使用img标签访问本地图片时产生Not allowed to load local resource的问题,在Spring Boot项目中创建配置类,实现WebMvcConfigurer接口并重写addResourceHandlers方法。

@Configuration
public class MyWebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/foodimg/**").addResourceLocations("file:" +
                "/C:/Users/N/Pictures");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/"); // 没有这句也不起作用
    }
}

然而前端使用img标签无法访问图片。
<img class="food-header-img" src="/foodimg/171223.jpg" />
image.png
控制台无报错。

请问有人能帮忙解决吗?
或者能帮忙解决Not allowed to load local resource的报错也行,感谢!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

请问怎么解决的呢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...