1.什么是thymeleaf
thymeleaf是页面模板引擎。类似于JSP
2.为什么使用thymeleaf
随着springboot的愈加流行,传统的使用JSP开发页面可能存在一些问题。
springboot 提供了一些模板引擎,thymeleaf, freemarker,velocity 等供我们选择。
其中因thymeleaf对spring MVC的良好支持,springboot推荐用thymeleaf作为模板引擎。
3.如何使用
- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.在html页面中引用
静态资源存放在 static 目录下
<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>thymeleaf的使用</title>
<link th:href="@{/css/animate.css}" rel="stylesheet" type="text/css" />
<script th:src="@{/js/common/jquery.min.js}"></script>
<script th:inline="javascript">
$(document).ready(function(){
var data = [[${result} ]] ; //js中获取后台传回来的数据
alert(data);
});
</script>
</head>
注意标签要闭合,布套后台处理时会报错。
3.后台通过springboot 使用@RestController 返回字符串即可