模板引擎Thymeleaf
在不使用springboot时我们需要把前端交给我们的html页面转成jsp页面(jsp也是一个模板引擎)
jsp好处是当我们查出一些数据转发到jsp页面以后,可以轻松实现数据的显示、交互等。同时jsp支持编写Java代码
但首先springboot是以jar的方式,而不是war;另外springboot是嵌入式的Tomcat,所以默认是不支持jsp的
模板引擎的作用:
聚合数据和模板并生成最终的 HTML 文档,处理器调用模板引擎来完成这一工作并将 HTML 文档作为响应实体发送给客户端
由此 我们引出Thymeleaf(springboot推荐):语法简单、功能强大
Thymeleaf 官网:https://www.thymeleaf.org/
Thymeleaf 在Github 的主页:(thymeleaf导入文档)https://github.com/thymeleaf/thymeleaf
Spring官方文档:(注意版本)https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter
引入:
springboot2.3以上版本使用thymeleaf不用导入pom依赖,直接导入thymeleaf的starter即可:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>