springBoot之静态资源

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29726359/article/details/88043603

springBoot之静态资源

使用SpringBoot

  • 创建SpringBoot应用,选中我们需要的模块。
    在这里插入图片描述在这里插入图片描述在这里插入图片描述
    创建项目时,注意项目名和放项目的文件夹不是一回事,放项目的文件夹需要自己创建。
    在这里插入图片描述

  • SpringBoot自动配置选中模块,我们只需要再配合文件中指定少量配置就可以运行起来。

  • 自己编写业务代码;

    • hello world示例

      • 项目结构
        在这里插入图片描述

      • 代码

        @Controller
        public class DemoController {
            @RequestMapping("/hello")
            @ResponseBody
            public String hello(){
                return "hello world";
            }
        }
        

SpringBoot对静态资源的映射规则

  • 所有/webjars/**,都去classpath:/META-INF/resourses/webjars/找资源;

    • webjars:以jar包的方式引入静态资源;

    • webjars网址:http://www.webjars.org
      在这里插入图片描述

    • 例如:localhost:8080/webjars/jquery/3.3.1-2/jquery.js

             <dependency>
                  <groupId>org.webjars</groupId>
                  <artifactId>jquery</artifactId>
                  <version>3.3.1-2</version>
              </dependency>
      
  • "/**"访问当前项目的任何资源(静态资源的文件夹)

    "Classpath:/META-INF/resources/",
    "Classpath:/resources/",
    "Classpath:/static",
    "Classpath:/public/",
    "/":当前目录的根路径
    

    Classpath:在哪些目录下可以找到您所要执行的Java程序所需要的类或者包\src\main\resources
    在这里插入图片描述
    localhost:8080/abc === 去静态资源文件夹里面找abc

  • 首页映射:静态资源文件夹下的所有index.html页面;被"/**"映射;

    localhost:8080/ 找index页面
    在这里插入图片描述

  • 配置每一页的小图标:所有的**/favicon.ico都是在静态资源文件夹下找

    • 首先清除浏览器缓存

    • 然后再重启运行

    • 刷新页面
      在这里插入图片描述

  • 自定义静态文件夹

    //在application.properties文件下配置:
    spring.resources.static-locations=classpath:/hello/,classpath:/kxy/
    //之前静态文件夹讲失效。
    

猜你喜欢

转载自blog.csdn.net/qq_29726359/article/details/88043603