一般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依赖

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

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"

最新文章

  1. [AC自动机]【学习笔记】
  2. C和指针 第三章 链接属性 extern、internal、none
  3. Win10 UI入门RelativePanel
  4. 我的微型工作流引擎-功能解析及API设计
  5. css3动画 bug 2点
  6. 利用快速排序原理找出数组中前n大的数
  7. [ios]iOS 图形编程总结
  8. ExtJS4.x 开发环境搭建
  9. Spring事务传递性探讨
  10. 使用cacti监控服务器
  11. centos7基础学习第一天
  12. Unity3D 代码入口
  13. Java数据结构与算法 - 外部存储
  14. cookie与sessionStorage机制
  15. Anaconda与Spyder升级命令
  16. fio 磁盘性能
  17. 自然语言处理之Levenshtien Distance算法研究
  18. Android 打开系统设置界面及相应的系统界面
  19. sprint2(第四天)
  20. “全栈2019”Java第二章:安装JDK11(Windows)

热门文章

  1. basicRF双向灯光控制
  2. 【T-SQL】基础——表别名
  3. Android 8.1 关机充电动画(一)模式选择
  4. JDBC08时间处理
  5. Kubernetes管理员手边必备的9个kubectl命令
  6. Java设计模式之建造者模式(Builder Pattern)
  7. 可能是把 Java 接口讲得最通俗的一篇文章
  8. Redis的几种集群方式分析
  9. 读懂操作系统(x64)之堆栈帧(过程调用)
  10. linux常用命令---centOS7的管理服务(针对yum安装的)