spring MVC是如何找到html文件并返回的?

在这里插入图片描述

Spring MVC 搜索路径

启动一个SpringBoot项目时,访问http://localhost:8080,对于SpringMVC,它会默认把这段url看成http://localhost:8080/index.html,所以这两个url是等价的。
.html, .css, .js, .img …都是静态资源文件,不是通过controller访问的,而是spring mvc有一个静态资源搜索路径,在spring boot中,是位于org.springframework.boot.autoconfigure.web.ResourceProperties类下

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{
"classpath:/META-INF/resources/",
 "classpath:/resources/", 
"classpath:/static/",
 "classpath:/public/"};

classpath表示类路径,项目编译后,会有一个classes文件,这个文件夹下就是类路径。
spring MVC会按顺序依次搜索/META-INF/resources/、/resources/、/static/、/public/文件夹,先搜到指定的静态资源,就直接返回,不会继续搜下去。
在这里插入图片描述
这里,静态资源搜索路径中有两个index.html。
根据先来后到的原则,由于 "classpath:/resources/"定义在"classpath:/static/"前面,所以搜索到classpath:/resources/"下的index就会返回。
在这里插入图片描述

自定义静态资源搜索路径

当然,Spring MVC静态资源搜索路径可以自定义,在application.yaml文件中配置:

spring:
  resources:
    static-locations: classpath:/custom

在这里插入图片描述
注意:
自定义的classpath:/custom这个路径并不会覆盖已经写死的静态资源访问路径,也就是前面几个路径依然生效。但是classpath:/custom是排在前面几个路径的前面的。

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-21 08:40:03       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-21 08:40:03       97 阅读
  3. 在Django里面运行非项目文件

    2024-03-21 08:40:03       78 阅读
  4. Python语言-面向对象

    2024-03-21 08:40:03       88 阅读

热门阅读

  1. RabbitMQ

    2024-03-21 08:40:03       44 阅读
  2. js数组去重常见方法

    2024-03-21 08:40:03       35 阅读
  3. 数据分析-Pandas数据分类处理

    2024-03-21 08:40:03       46 阅读
  4. React.js快速入门教程

    2024-03-21 08:40:03       43 阅读
  5. bert源码分析之tokenization

    2024-03-21 08:40:03       35 阅读
  6. 【Qt之·类QProcess】

    2024-03-21 08:40:03       42 阅读
  7. pytest相关面试题

    2024-03-21 08:40:03       39 阅读
  8. 【WEEK4】学习目标及总结【SpringMVC】【中文版】

    2024-03-21 08:40:03       41 阅读
  9. 理论学习:深度学习里什么是置信度

    2024-03-21 08:40:03       36 阅读
  10. C# 判断变量类型 GetType 未解决

    2024-03-21 08:40:03       39 阅读
  11. 洛谷入门——P1307 [NOIP2011 普及组] 数字反转

    2024-03-21 08:40:03       37 阅读