欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

深入浅析springboot中static和templates区别

时间:2021-06-11 08:10:24|栏目:JAVA代码|点击:

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

1.1 在static下新建hello1.html

运行程序,浏览器输入http://localhost:8080/hello1.html

在这里插入图片描述

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

1.2 也可以通过接口跳转
1.2.1 添加接口

@RequestMapping("hello1")
public String hello1() {
  return "hello1.html";
}

1.2.2 注释掉thymeleaf依赖

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

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

在这里插入图片描述

2.template目录
2.1 在template下新建hello2.html
运行程序,浏览器输入http://localhost:8080/hello2.html

在这里插入图片描述

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

2.2 通过接口访问
2.2.1 添加接口

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

@RequestMapping("hello2")
public String hello2() {
  return "hello2";
}

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

在这里插入图片描述

3.结束语

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

4.总结:

改bug需要用心,掉头发是不可避免滴!!!

上一篇:Java数据封装树形结构代码实例

栏    目:JAVA代码

下一篇:java实现抖音飞机大作战

本文标题:深入浅析springboot中static和templates区别

本文地址:http://www.codeinn.net/misctech/139779.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有