当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot静态资源目录访问

SpringBoot静态资源目录访问

2019年07月19日  | 移动技术网IT编程  | 我要评论

腾讯电话人工服务,南浔电影院,鼓上蚤时迁电影

静态资源配置

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

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

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

总结

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网