SpringBoot静态资源目录访问

时间:2021-05-20

静态资源配置

创建一个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 */@Configurationpublic 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 */@Configurationpublic 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目录

总结

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章