亲宝软件园·资讯

展开

静态资源目录访问 SpringBoot静态资源目录访问

staHuri 人气:0

静态资源配置

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

总结

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

加载全部内容

相关教程
猜你喜欢
用户评论