静态资源配置
创建一个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目录
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对亿速云的支持。如果你想了解更多相关内容请查看下面相关链接
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。