欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot静态资源目录访问

程序员文章站 2024-02-26 11:44:04
静态资源配置 创建一个staticconfig 继承 webmvcconfigureradapter package com.huifer.blog.confi...

静态资源配置

创建一个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);
  }
}

SpringBoot静态资源目录访问

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

SpringBoot静态资源目录访问

创建一个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目录

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接