SpringBoot静态资源映射 目录12 静态资源映射12.1 源码12.2 静态资源路径映射规则12.2.1 WebJars 映射规则/webjars/** 路径12.2.2 静态资源文件夹映射规则/** 路径12.2.3 欢迎页规则12 静态资源映射12.1 源码注代码是Springboot2.0.1的代码ConfigurationProperties(prefix spring.resources, ignoreUnknownFields false)public class ResourceProperties implements ResourceLoaderAware {//可以设置和静态资源有关的参数缓存时间等WebMvcAuotConfiguration Override public void addResourceHandlers(ResourceHandlerRegistry registry) { if (!this.resourceProperties.isAddMappings()) { logger.debug(Default resource handling disabled); return; } Integer cachePeriod this.resourceProperties.getCachePeriod(); if (!registry.hasMappingForPattern(/webjars/**)) { customizeResourceHandlerRegistration(registry.addResourceHandler(/webjars/**).addResourceLocations(classpath:/META‐INF/resources/webjars/).setCachePeriod(cachePeriod)); } String staticPathPattern this.mvcProperties.getStaticPathPattern(); //静态资源文件夹映射 if (!registry.hasMappingForPattern(staticPathPattern)) { customizeResourceHandlerRegistration(registry. addResourceHandler(staticPathPattern).addResourceLocations(this.resourceProperties.getStaticLocations()).setCachePeriod(cachePeriod)); } } / /配置欢迎页映射 Bean public WelcomePageHandlerMapping welcomePageHandlerMapping( ResourceProperties resourceProperties) { return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(), this.mvcProperties.getStaticPathPattern()); } //配置喜欢的图标 Configuration ConditionalOnProperty(value spring.mvc.favicon.enabled, matchIfMissing true) public static class FaviconConfiguration { private final ResourceProperties resourceProperties; public FaviconConfiguration(ResourceProperties resourceProperties) { this.resourceProperties resourceProperties; } Bean public SimpleUrlHandlerMapping faviconHandlerMapping() { SimpleUrlHandlerMapping mapping new SimpleUrlHandlerMapping(); mapping.setOrder(Ordered.HIGHEST_PRECEDENCE 1); //所有 **/favicon.ico mapping.setUrlMap(Collections.singletonMap(**/favicon.ico, faviconRequestHandler()); return mapping; } Bean public ResourceHttpRequestHandler faviconRequestHandler() { ResourceHttpRequestHandler requestHandler new ResourceHttpRequestHandler(); requestHandler.setLocations(this.resourceProperties.getFaviconLocations()); return requestHandler; } }12.2 静态资源路径映射规则12.2.1 WebJars 映射规则/webjars/** 路径WebJars 会将前端静态资源如 jQuery、Bootstrap 等打包成 Jar 包让 Java 项目可以通过 Maven 依赖引入。Spring Boot 对 WebJars 的映射规则是所有 /webjars/** 请求都会去 classpath:/META-INF/resources/webjars/ 下如下图找资源。在 pom.xml 中添加 jQuery 的 WebJars 依赖dependency groupIdorg.webjars/groupId artifactIdjquery/artifactId version3.3.1/version /dependency引入依赖后启动 Spring Boot 项目后在浏览器中访问 http://localhost:8080/webjars/jquery/3.3.1/jquery.js如果能成功打开 jQuery 的源码文件说明 WebJars 映射规则生效 ——Spring Boot 自动找到了 classpath:/META-INF/resources/webjars/jquery/3.3.1/jquery.js 这个资源。12.2.2 静态资源文件夹映射规则/** 路径Spring Boot 对普通静态资源如自定义的 js、css、html 等的映射规则是所有 /** 请求会依次去以下静态资源文件夹找资源优先级从左到右递减classpath:/META-INF/resources/classpath:/resources/classpath:/static/classpath:/public/注classpath指的是src/main/resources/验证在 src/main/resources 下创建不同的静态资源文件夹并分别放入同名文件 test.txt内容区分开方便验证优先级src/main/resources/META-INF/resources/test.txt内容写 META-INF/resourcessrc/main/resources/resources/test.txt内容写 resourcessrc/main/resources/static/test.txt内容写 staticsrc/main/resources/public/test.txt内容写 public步骤 2访问测试文件启动项目后在浏览器中访问 http://localhost:8080/test.txt最终会看到内容为 META-INF/resources 的文件 —— 这验证了静态资源文件夹的优先级顺序META-INF/resources 优先级最高。如果按优先级从高到低依次删除文件即可验证所有的优先级。步骤 3验证自定义静态资源访问再创建一个自定义的静态资源比如在 src/main/resources/static/ 下创建 myjs.js内容为 hellooooooo。访问 http://localhost:8080/myjs.js如果能看到源码说明 /** 映射规则生效Spring Boot 从 static 文件夹中找到了该资源。12.2.3 欢迎页规则Spring Boot 的欢迎页规则是访问 localhost:8080/ 时会自动找静态资源文件夹下的 index.html。优先级同12.2.2。验证步骤同12.2.2。启动项目后直接访问 http://localhost:8080/如果能看到index.html页面说明欢迎页规则生效 ——Spring Boot 自动找到了index.html。

相关新闻

最新新闻

PinWin终极指南:3种方法让Windows窗口永远置顶,工作效率提升200%

PinWin终极指南:3种方法让Windows窗口永远置顶,工作效率提升200%

PinWin终极指南:3种方法让Windows窗口永远置顶,工作效率提升200% 【免费下载链接】PinWin Pin any window to be always on top of the screen 项目地址: https://gitcode.com/gh_mirrors/pin/PinWin 在Windows多任务处理的日常中,你是…

2026/8/3 10:53:53
访问者模式:解耦数据结构与操作的优雅方案

访问者模式:解耦数据结构与操作的优雅方案

1. 为什么你的数据对象越来越臃肿?上周review同事的代码时,我发现一个User类竟然有37个方法——从基础的getName()到业务级的calculateLoyaltyPoints(),甚至还有generateMonthlyReport()这种明显不该属于用户对象的方法。这让我想起自己刚入行…

2026/8/3 10:53:53
抖音无水印下载器终极指南:从零开始掌握批量下载与智能管理

抖音无水印下载器终极指南:从零开始掌握批量下载与智能管理

抖音无水印下载器终极指南:从零开始掌握批量下载与智能管理 【免费下载链接】douyin-downloader A practical Douyin downloader for both single-item and profile batch downloads, with progress display, retries, SQLite deduplication, and browser fallback …

2026/8/3 10:53:53
Mate Engine:5分钟快速上手的免费开源桌面伴侣终极指南

Mate Engine:5分钟快速上手的免费开源桌面伴侣终极指南

Mate Engine:5分钟快速上手的免费开源桌面伴侣终极指南 【免费下载链接】Mate-Engine A free Desktop Mate alternative with a lightweight interface and custom VRM support, though with more features. 项目地址: https://gitcode.com/gh_mirrors/ma/Mate-En…

2026/8/3 10:53:53
Flutter与OpenHarmony开发实战:移动数据监管App

Flutter与OpenHarmony开发实战:移动数据监管App

1. 项目背景与核心价值在移动应用开发领域,跨平台框架Flutter与开源操作系统OpenHarmony的结合正在开辟新的技术路径。这个实战项目聚焦于开发一款移动数据使用监管助手App的个人中心模块,其核心价值在于:验证Flutter在OpenHarmony生态中的完…

2026/8/3 10:53:53
Cheat Engine逆向工程实战:从内存扫描到代码注入的完整指南

Cheat Engine逆向工程实战:从内存扫描到代码注入的完整指南

1. 从“作弊”到“逆向”:Cheat Engine的深度价值再认识 一提到Cheat Engine,很多人的第一反应是“游戏修改器”。没错,它最初确实是为了修改单机游戏的内存数据,实现“无敌”、“无限金币”这类功能而诞生的。但如果你仅仅把它当…

2026/8/3 10:48:52