springboot静态资源访问,总是忘记,记录一下

一:在resource不同目录下创建三个*.html静态文件,
在这里插入图片描述

方式一:直接访问html文件(默认只能访问static目录下文件)

访问hello1.html		http://localhost:8080/hello1.html;可以直接访问

方式二:通过配置文件application.yml访问

//配置文件:
spring:
  mvc:
    static-path-pattern: /**   //经过这个路径的请求都可以获取静态资源
  resources:
    static-locations: classpath:/static,classpath:/templates,classpath:/    //所有的静态位置都监听
获取静态资源:    
访问hello1.html   	http://localhost:2222/hello1.html
访问hello11.html  	http://localhost:2222/hello11.html
访问hello3.html		http://localhost:2222/hello3.html

方式三:通过Controller访问静态文件(默认只能访问static里面的静态资源)

 @GetMapping("/index")
    public String ToIndex() {
        return "hello1.html";
    }
    
    访问hello1.html    http://localhost:2222/index

方式四 :通过controller+配置文件application.yml访问.html文件

<application.yml>
spring:
  mvc:
    view:
      prefix: /
      suffix: .html
  resources:
    static-locations: classpath:/static,classpath:/templates,classpath:/
    
访问地址:	http://localhost:2222/index

<controller1-访问hello1.html>
 @GetMapping("/index")
    public String ToIndex() {
        return "hello1.html";
    }
    
 <controller2-访问hello11.html>
 @GetMapping("/index")
    public String ToIndex() {
        return "hello11.html";
    }   
    
    <controller3-访问hello3.html>
 @GetMapping("/index")
    public String ToIndex() {
        return "hello3.html";
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值