SpringBoot支持的视图技术之Thymeleaf
1、SpringBoot可整合的模板引擎技术
(1)FreeMarker
(2)Groory
(3)Thymeleaf
(4)Mustache
等等
2、Thymeleaf常用标签(示例代码)
<!DOCTYPE html>
<html lang = "en" xmlns:th="http://www.thymeleaf.org"> #引入thymeleaf标签
<head>
<meta charset = "UTF-8">
<meta name = "viewport" content = "width = device - width, initial - scale = 1.0">
<meta http-equiv = "X-UA-Compatible" content = "ie-edge">
<link rel="stylesheet" type="text/css" media="all" href="../../css/gtvg.css" th:href="@{/css/gtvg.css}"/> #引入外联样式文件
<title>Title</title>
</head>
<body>
<p th:text="#{hello}">Hello world</p>
</body>
</html>
3、Thymeleaf主要语法
变量表达式
${...} //获取上下文中的变量值
选择变量表达式
*{...} //用于从被选定的对象获取属性值
消息表达式
#{...} //用于Thymeleaf模板页面国际化内容的动态替换和展示
链接URL表达式
@{...} //用于页面跳转或者资源的引入
片段表达式
~{...} //用来标记一个片段模板,并根据需要移动或传递给其他模板
4、Thymeleaf基本使用
4.1 在SpringBoot项目中使用Thymeleaf模板,必须保证引入Thymeleaf依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf<artifactId>
</dependency>
4.2 其次在全局配置文件中配置Thymeleaf模板的一些参数。(如设置模板缓存、模板编码、模板样式、指定模板页面存放路径、指定模板页面名称的后缀)
#模板缓存开启
spring.thymeleaf.cache=true
#模板编码
spring.thymeleaf.encoding=UTF-8
#模板样式
spring.thymeleaf.mode=HTML5
#指定模板页面存放路径
spring.thymeleaf.prefix=classpath:/templates/
#指定模板页面名称的后缀
spring.thymeleaf.suffix=.html