SpringBoot静态资源目录访问
作者:staHuri
今天小编就为大家分享一篇关于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); } }
翻看源码发实现了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目录
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接