springboot返回html页面原理,SpringBoot返回html页面

一般Controller返回数据或页面,今天谈一下返回页面的场景。

一.不使用template

1. controller中定义对应的访问路由及返回的页面(使用Controller,不要使用RestController),如:

@GetMapping("/hello")

public String test2() {

return "hello";

}

2.在SpringBoot配置文件中配置SpringMVC

spring:

mvc:

view:

prefix:/suffix: .html

3.html文件配置路径。

静态文件要放在SpringBoot默认的加载路径下(SpringBoot中的src/main/resources/文件夹对应classpath:):

classpath:/META-INF/resources、classpath:/resources、classpath:/static、classpath:/public

二.使用thymeleaf

1.引入thymeleaf依赖

org.springframework.boot

spring-boot-starter-thymeleaf

2.html放在classpath:/templates下。如果html都是放在templates下,SpringBoot的配置文件不要配置,因为默认配置就是这个路径。

3.如果要自定义需要在SpringBoot配置文件中自定义配置。

spring:

thymeleaf:

suffix: .html

prefix: classpath:/xx/xx/

如果有更深层的路径,可以在controller的返回值拼上对应的html路径。

如配置为:prefix: classpath:/templates/,现要返回templates/order/order.html,controller就要返回 "order/order"

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值