SpringBoot中static与templates有何本质区别?

2026-05-28 12:441阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计436个文字,预计阅读时间需要2分钟。

SpringBoot中static与templates有何本质区别?

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

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

1.1 在static下新建hello1.html

运行程序,浏览器输入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 浏览器输入localhost:8080/hello1

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

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

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

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

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

2.2.2 浏览器输入localhost:8080/hello2

SpringBoot中static与templates有何本质区别?

3.结束语

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

4.总结:

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

到此这篇关于深入浅析springboot中static和templates区别的文章就介绍到这了,更多相关springboot static和templates区别内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计436个文字,预计阅读时间需要2分钟。

SpringBoot中static与templates有何本质区别?

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

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

1.1 在static下新建hello1.html

运行程序,浏览器输入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 浏览器输入localhost:8080/hello1

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

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

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

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

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

2.2.2 浏览器输入localhost:8080/hello2

SpringBoot中static与templates有何本质区别?

3.结束语

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

4.总结:

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

到此这篇关于深入浅析springboot中static和templates区别的文章就介绍到这了,更多相关springboot static和templates区别内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!