0
点赞
收藏
分享

微信扫一扫

springboot静态资源的映射规则


SpringBoot项目的静态资源应该放在哪里,又该如何访问?从静态资源访问方式来分,可以分两类:

/webjars/**:在classpath:/META-INF/resources/webjars/ 里找资源

WebJars是打包成jar文件的客户端web库(如jQuery和Bootstrap)。使用基于JVM的构建工具(如Maven、Gradle、sbt…)来下载客户端的依赖。

所有webjars都可以在​​https://www.webjars.org/​​此网站找到。
下面以添加jquery来举个例子来说明这一种方式:

1、首先,项目的pom.xml添加jquery的依赖。

<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>

2、因为jquery.js在META-INF/resources/webjars/jquery/3.4.1/路径下:

springboot静态资源的映射规则_jquery

所以要访问jquery.js的web访问路径是:

http://localhost:9999/webjars/jquery/3.4.1/jquery.js

/**:在项目指定的静态资源的文件夹里找资源,如下面这四个位置

上面是通过添加依赖的,那么如果是我们自己写的html、js、css呢?这些资源可以放在以下几个地方

  1. classpath:/resources/
    如:MySpringboot/src/main/resources/hello.html 访问方式如下:

http://localhost:9999/hello.html

  1. classpath:/META-INF/resources/
    如:MySpringboot/src/main/resources/META-INF/resources/hello.html,那么就可以用如下方式访问

http://localhost:9999/hello.html

  1. classpath:/static/
    如:/home/kyun/Desktop/MySpringboot/src/main/resources/static/hello1.html,访问方式:

http://localhost:9999/hello1.html

  1. classpath:/public/
    如:/home/kyun/Desktop/MySpringboot/src/main/resources/public/hello1.html,访问方式:

http://localhost:9999/hello1.html

欢迎页,即静态资源文件夹(上面四个位置)下的index.html

访问方式:

http://localhost:9999/

所有的**/favicon.ico都是在静态资源文件夹下找

如果不想使用这四个默认的位置,可以在application.properties修改,如:

spring.resources.static-locations=classpath:/hello/,classpath:/vivi,classpath:keke

以上就是全部内容了。谢谢。


举报

相关推荐

0 条评论