Spring Boot图解静态资源映射规则

通过jar包引入静态资源

点击 webJars官网,在里面可以找到绝大多数的静态资源的jar包,并可通过maven引入项目
Spring Boot图解静态资源映射规则
引入项目后,Spring Boot对此种方式添加了默认资源映射方式。

Spring Boot图解静态资源映射规则

路径 /webjars/** 会被映射到 classpath:/META-INF/resources/webjars/
在上图中,我可以通过路径 /webjars/bootstrap/4.5.0/css/bootstrap.css
访问到我们上图所导入的bootstrap包中的bootstrap.css

对于我们自己的静态资源,可以放置在如下位置

  • classpath:/META-INF/resources/
  • classpath:/resources/
  • classpath:/static/
  • classpath:/public/
    若一个路径没有被处理,Spring Boot 将会到上述位置去寻找匹配项。

例如: 路径 /hello/helloword.html ,Spring Boot 将会到"classpath:/META-INF/resources/",“classpath:/resources/”, “classpath:/static/”, “classpath:/public/”,等文件夹下寻找是否有hello/helloword.html文件

欢迎页

对于路径 / ,Spring Boot会到上述静态资源目录,以及classpath:/templates下去寻找名为index.html的文件当作欢迎页