Springboot的web开发中static和templates的区别

在src/main/resources下面有两个文件夹,static和templates,springboot默认 static中放静态页面,而templates中放动态页面

1.新建一个SpringBoot Maven项目

1.1 依赖设置(选中web和Thymeleaf)

这里写图片描述
这里写图片描述

1.2 resource结构如下

这里写图片描述

2. static目录

2.1 在static下新建hello1.html

运行程序,浏览器输入http://localhost:8080/hello1.html
这里写图片描述

so,可以在根目录下访问hello1.html,static目录类似于传统Java web中的webroot或webcontent

2.2 也可以通过接口跳转

2.2.1 添加接口

    @RequestMapping("hello1")
    public String hello1() {

        return "hello1.html";
    }

2.2.2 注释掉thymeleaf依赖

    <dependencies>
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-thymeleaf</artifactId>-->
        <!--</dependency>-->

2.2.3 浏览器输入http://localhost:8080/hello1

这里写图片描述

3. template目录

3.1 在template下新建hello2.html

运行程序,浏览器输入http://localhost:8080/hello2.html
这里写图片描述

templates下的动态页面不能直接访问

3.2 通过接口访问

3.2.1 添加接口

注意接口中return的页面不包含.html后缀

    @RequestMapping("hello2")
    public String hello2() {

        return "hello2";
    }

3.2.2 浏览器输入http://localhost:8080/hello2

这里写图片描述

4.总结

静态页面的return默认是跳转到/static/目录下,当在pom.xml中引入了thymeleaf组件,动态跳转会覆盖默认的静态跳转,默认就会跳转到/templates/下,注意看两者return代码也有区别,动态没有html后缀。

  • 15
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值