评论

收藏

[Java] SpringBoot静态资源目录访问

编程语言 编程语言 发布于:2021-10-06 12:05 | 阅读数:481 | 评论:0

今天小编就为大家分享一篇关于SpringBoot静态资源目录访问,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
静态资源配置
创建一个staticconfig 继承 webmvcconfigureradapter
package com.huifer.blog.config;
import org.springframework.context.annotation.configuration;
import org.springframework.web.servlet.config.annotation.resourcehandlerregistry;
import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter;
/**
 * 描述:
 * 静态文件配置
 * @author huifer
 * @date 2019-01-01
 */
@configuration
public class staticconfig extends webmvcconfigureradapter {
  public void addresourcehandlers(resourcehandlerregistry registry) {
  registry.addresourcehandler("/js/**").addresourcelocations("classpath:/static/js/");
  registry.addresourcehandler("/css/**").addresourcelocations("classpath:/static/css/");
  registry.addresourcehandler("/fonts/**").addresourcelocations("classpath:/static/fonts/");
  registry.addresourcehandler("/images/**").addresourcelocations("classpath:/static/images/");
  super.addresourcehandlers(registry);
  }
}
DSC0000.png

翻看源码发实现了webmvcconfigurer 故而修改
DSC0001.png

创建一个staticconfig 实现 webmvcconfigurer
package com.huifer.blog.config;
import org.springframework.context.annotation.configuration;
import org.springframework.web.servlet.config.annotation.resourcehandlerregistry;
import org.springframework.web.servlet.config.annotation.webmvcconfigurer;
/**
 * 描述:
 * 静态文件配置
 * @author huifer
 * @date 2019-01-01
 */
@configuration
public class staticconfig implements webmvcconfigurer {
  public void addresourcehandlers(resourcehandlerregistry registry) {
  registry.addresourcehandler("/js/**").addresourcelocations("classpath:/static/js/");
  registry.addresourcehandler("/css/**").addresourcelocations("classpath:/static/css/");
  registry.addresourcehandler("/fonts/**").addresourcelocations("classpath:/static/fonts/");
  registry.addresourcehandler("/images/**").addresourcelocations("classpath:/static/images/");
//  super.addresourcehandlers(registry);
  }
}
修改pom 文件
<resources>
    <resource>
<directory>src/main/resources</directory>
    <filtering>true</filtering>
    </resource>
  </resources>
以上三种方案都可以访问到 static目录
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对CodeAE代码之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/staHuri/article/details/85561243

关注下面的标签,发现更多相似文章