• Springboot-静态资源访问


    Springboot-静态资源访问

    1.官方文档

    1.在线文档

    https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.developing-web-applications

    image-20220731153030994

    image-20220731153037729

    2.基本介绍

    1. 只要静态资源放在类路径下: /static 、 /public 、 /resources 、 /META-INF/resources 可以被直接访问- 对应文件 WebProperties.java

    直接放到resources目录下是访问不到的,这里的 /resources是指在resource目录的创建resources目录

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
    
    • 1
    1. 常见静态资源:JS、CSS 、图片(.jpg .png .gif .bmp .svg)、字体文件(Fonts)等

    2. 访问方式 :默认: 项目根路径/ + 静态资源名 比如 http://localhost:8080/hi.jpg . - 设置 WebMvcProperties.java

    /**
     * Path pattern used for static resources.
     */
    private String staticPathPattern = "/**";
    
    • 1
    • 2
    • 3
    • 4

    3.快速入门

    1.创建 SpringBoot 项目 springbootweb

    image-20220731154131281

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0modelVersion>
    
       <groupId>com.llpgroupId>
       <artifactId>springBootwebartifactId>
       <version>1.0-SNAPSHOTversion>
    
       
       <parent>
          <artifactId>spring-boot-starter-parentartifactId>
          <groupId>org.springframework.bootgroupId>
          <version>2.5.3version>
       parent>
    
       <dependencies>
          <dependency>
             <groupId>org.springframework.bootgroupId>
             <artifactId>spring-boot-starter-webartifactId>
          dependency>
       dependencies>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    2.创建相关静态资源目录, 并放入测试图片, 没有目录,自己创建即可, 完成测试

    image-20220731154350413

    image-20220731154327204

    4.静态资源访问注意事项和细节

    1. 静态资源访问原理:静态映射是 /**, 也就是对所有请求拦截,请求进来,先看 Controller 能不能处理,不能处理的请求交给静态资源处理器,如果静态资源找不到则响应 404 页面

    image-20220731154622296

    1. 改变静态资源访问前缀,比如我们希望 http://localhost:8080/llp/* 去请求静态资源, 应用场景:静态资源访问前缀和控制器请求路径冲突

    (1)创建src\main\resources\application.yml

    spring:
      mvc:
        static-path-pattern: /llp/**
    
    • 1
    • 2
    • 3

    (2)重启应用,完成测试, 浏览器输入: http://localhost:8080/llp/4.jpg

    image-20220731155023567

    1. 改变默认的静态资源路径,比如希望在类路径下增加 llpimg 目录 作为静态资源路径 , 并完成测试.

    (1)如图所示

    image-20220731155724430

    (2)配置src\main\resources\application.yml

    spring:
      mvc:
        static-path-pattern: /llp/**
      web:
        resources:
          #修改/指定 静态资源的访问路径/位置
          #
          static-locations: ["classpath:/llpimg/","classpath:/META-INF/resources/",
                             "classpath:/resources/", "classpath:/static/", "classpath:/public/"]      #String[] staticLocations
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    (3)测试访问http://localhost:8080/llp/5.png

    image-20220731160243754

    (4)如果你配置 static-locations, 原来的访问路径就被覆盖,如果需要保留,你再指定一下即可

    spring:
      mvc:
        static-path-pattern: /llp/**
      web:
        resources:
          #修改/指定 静态资源的访问路径/位置
          #
          static-locations: ["classpath:/llpimg/","classpath:/META-INF/resources/",
                             "classpath:/resources/", "classpath:/static/", "classpath:/public/"]      #String[] staticLocations
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    Python + Jmeter 实现自动化性能压测
    状态管理的艺术:探索Flutter的Provider库
    Anaconda配置镜像源
    如何快速集成Android版Serverless认证服务-手机号码篇
    torch.nn.init.kaiming_normal_
    scratch绘制彩虹灯柱 2023年9月中国电子学会图形化编程 少儿编程 scratch编程等级考试三级真题和答案解析
    2299: 计算并输出学生成绩
    Rabbitmq 的管理配置
    数学建模之圈养湖羊的空间利用率
    大名鼎鼎的OceanBase居然在买Star了?
  • 原文地址:https://blog.csdn.net/qq_44981526/article/details/126086752