SpringBoot静态资源目录访问
程序员文章站
2024-03-02 22:30:04
静态资源配置
创建一个staticconfig 继承 webmvcconfigureradapter
package com.huifer.blog.confi...
静态资源配置
创建一个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目录
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
推荐阅读