当前位置: 首页 > news >正文

Spring MVC 和 Spring Boot 是如何访问静态资源的?

Spring MVC 和 Spring Boot 在配置静态资源访问方面有所不同,Spring Boot 提供了更便捷的自动配置。

一、Spring Boot 如何配置静态资源访问 (推荐方式)

Spring Boot 遵循“约定优于配置”的原则,对静态资源的访问提供了非常方便的自动配置。

  1. 默认静态资源位置:
    Spring Boot 会自动从以下类路径 (classpath) 位置查找静态资源:

    • classpath:/META-INF/resources/
    • classpath:/resources/
    • classpath:/static/ (常用)
    • classpath:/public/ (常用)

    优先级顺序: META-INF/resources/ > resources/ > static/ > public/
    这意味着如果你在 src/main/resources/static/ 目录下放一个 style.css 文件,那么你可以通过 http://localhost:8080/style.css 来访问它。

  2. WebJars 支持:
    Spring Boot 也自动支持 WebJars。如果你的项目中引入了 WebJars 依赖(例如 Bootstrap, jQuery),它们会自动从 classpath:/META-INF/resources/webjars/ 提供服务。
    例如,如果引入了 bootstrap.jar,那么它的资源可以通过 http://localhost:8080/webjars/bootstrap/{version}/css/bootstrap.css 访问。

  3. 自定义静态资源位置:
    如果你想使用其他位置,可以在 application.propertiesapplication.yml 中配置:

    # application.properties
    spring.web.resources.static-locations=classpath:/my-custom-static/,classpath:/another-location/
    # 注意:这会覆盖默认位置,如果你还想使用默认位置,需要显式包含它们,例如:
    # spring.web.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/my-custom-static/
    
    # application.yml
    spring:web:resources:static-locations:- classpath:/my-custom-static/- classpath:/another-location/# - classpath:/static/ # 如果覆盖了默认,想保留默认的需要加上
    
  4. 自定义静态资源访问路径前缀:
    默认情况下,静态资源直接从根路径 (/) 开始访问。你可以修改这个前缀:

    # application.properties
    spring.mvc.static-path-pattern=/static-assets/**
    
    # application.yml
    spring:mvc:static-path-pattern: /static-assets/**
    

    配置后,src/main/resources/static/style.css 对应的访问路径将变为 http://localhost:8080/static-assets/style.css

  5. 缓存控制:
    Spring Boot 允许你配置静态资源的缓存策略:

    # application.properties
    spring.web.resources.cache.period=3600 # 缓存周期,单位秒。设为0禁用缓存。
    spring.web.resources.cache.cachecontrol.max-age=3600
    spring.web.resources.cache.cachecontrol.must-revalidate=true
    # ... 其他 Cache-Control 指令
    
  6. Resource Chain (资源链) - 高级:
    Spring Boot 支持资源链,可以用于版本化资源(在文件名中加入hash值以实现缓存清除)等。

    # application.properties
    spring.web.resources.chain.enabled=true # 通常默认是true
    spring.web.resources.chain.strategy.content.enabled=true # 基于内容的版本策略
    spring.web.resources.chain.strategy.content.paths=/**
    spring.web.resources.chain.strategy.fixed.enabled=true # 固定版本策略
    spring.web.resources.chain.strategy.fixed.paths=/js/lib/
    spring.web.resources.chain.strategy.fixed.version=1.0.0
    

    启用后,你可以在模板(如Thymeleaf)中使用 @{} 语法来获取版本化的URL:<link th:href="@{/css/style.css}" rel="stylesheet"> 会被解析为类似 /css/style-a2c4e1f.css 的路径。

二、“传统” Spring MVC 如何配置静态资源访问

在不使用Spring Boot自动配置的传统Spring MVC项目中,你需要显式配置静态资源处理器。

  1. XML 配置 (spring-mvc-config.xml 或类似文件):
    你需要告诉 DispatcherServlet 将特定模式的请求交给默认的 Servlet 处理器或者专门的资源处理器来处理静态资源,而不是尝试寻找 Controller。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 启用注解驱动的MVC --><mvc:annotation-driven /><!-- 扫描Controller --><context:component-scan base-package="com.example.controller" /><!-- 静态资源处理器 --><!-- mapping: 对外暴露的访问路径 --><!-- location: 静态资源在项目中的实际位置 (可以是类路径、文件系统路径或WEB-INF下) --><mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" cache-period="31556926" /><mvc:resources mapping="/static/**" location="classpath:/static/" /><mvc:resources mapping="/images/**" location="file:///opt/myapp/images/" /><!-- 如果你的DispatcherServlet映射的是"/" (拦截所有请求),你需要这个来让容器的默认Servlet处理未被Spring MVC处理的静态资源请求.这对于直接放在webapp根目录下的静态资源有用(不推荐)。 --><mvc:default-servlet-handler /><!-- 视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean>
    </beans>
    
    • <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />:
      • mapping: 当请求URL以 /resources/ 开头时,例如 http://localhost:8080/myapp/resources/css/style.css
      • location: Spring会去 webapp/WEB-INF/resources/ 目录下查找对应的 css/style.css 文件。
      • classpath:/static/: 表示从类路径下的 static 目录查找。
    • <mvc:default-servlet-handler />: 如果 DispatcherServlet 映射了 / (即处理所有请求),这个配置会允许容器的默认Servlet处理那些没有被Spring MVC Controller或 <mvc:resources> 匹配到的请求。这通常用于服务那些直接放在 webapp 根目录下的静态文件。
  2. Java 配置 (使用 WebMvcConfigurer):
    如果你使用Java配置,你需要创建一个实现 WebMvcConfigurer 接口的配置类。

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;@Configuration
    @EnableWebMvc // 启用Spring MVC (等同于XML中的 <mvc:annotation-driven />)
    @ComponentScan(basePackages = "com.example.controller")
    public class WebConfig implements WebMvcConfigurer {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/resources/**") // 对外暴露的访问路径.addResourceLocations("/WEB-INF/resources/") // 实际资源位置.setCachePeriod(31556926); // 缓存周期 (秒)registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");registry.addResourceHandler("/images/**").addResourceLocations("file:///opt/myapp/images/"); // 文件系统路径// WebJars 支持 (手动配置)registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}// 配置默认的Servlet来处理静态文件 (等同于XML中的 <mvc:default-servlet-handler />)@Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {configurer.enable();}@Beanpublic InternalResourceViewResolver viewResolver() {InternalResourceViewResolver resolver = new InternalResourceViewResolver();resolver.setPrefix("/WEB-INF/jsp/");resolver.setSuffix(".jsp");return resolver;}
    }
    

总结:

  • Spring Boot: 推荐方式。通过自动配置,你只需要将静态资源放在约定的目录 (src/main/resources/static, src/main/resources/public 等) 即可,几乎零配置。自定义也非常简单,通过 application.properties/yml 文件即可。
  • Spring MVC (传统): 需要显式配置。通过XML (<mvc:resources>) 或Java配置 (WebMvcConfigureraddResourceHandlers 方法) 来映射URL到静态资源的位置。

对于新项目,强烈建议使用Spring Boot,因为它极大地简化了包括静态资源在内的各种配置。

http://www.xdnf.cn/news/405757.html

相关文章:

  • MySQL数据库表的约束
  • 反弹shell再入门
  • MySQL查询优化100条军规
  • 深度解析RagFlow:本地大模型驱动的高效知识库应用搭建指南
  • Java MVC
  • nRF5_SDK_17.1.0_ddde560之ble_app_uart_c 出错
  • [Java实战]Spring Boot 整合 Session 共享(十七)
  • LintCode第42题-最大子数组 II
  • 《Vuejs设计与实现》第 5 章(非原始值响应式方案) 中
  • OpenCV 的 CUDA 模块中用于将一个多通道 GpuMat 图像拆分成多个单通道图像的函数split()
  • 【AI News | 20250512】每日AI进展
  • 一键生成达梦、Oracle、MySQL 数据库 ER 图!解锁高效数据库设计!
  • 【LeetCode】49.字母异位词分组
  • 典籍知识问答重新生成和消息修改Bug修改
  • 从零搭建AI工作站:Gemma3大模型本地部署+WebUI配置全套方案
  • sqlmap使用入门
  • Linux 系统中设置开机启动脚本
  • AAAI-2025 | 中科院无人机导航新突破!FELA:基于细粒度对齐的无人机视觉对话导航
  • 【JAVA】业务系统订单号,流水号生成规则工具类
  • python练习-20250512
  • C++23 views::slide (P2442R1) 深入解析
  • AnaTraf:深度解析网络性能分析(NPM)
  • C语言:深入理解指针(3)
  • 基于 Nexus 在 Dockerfile 配置 yum, conda, pip 仓库的方法和参考
  • T2000云腾边缘计算盒子在数猪场景中的应用|YOLOv8+NodeRED
  • 湖北理元理律师事务所:企业债务危机的“止血”与“造血”平衡术
  • 01背包和完全背包
  • 基于Qt6 + MuPDF在 Arm IMX6ULL运行的PDF浏览器——MuPDF Tools
  • 大项目k8s集群有多大规模,多少节点,有多少pod
  • 智能指针入门:深入理解 C++ 的 shared_ptr