Spring Boot之Thymeleaf开发
1、Thymeleaf模板引擎
Spring Boot 提供了大量的模板引擎,包括FreeMarker\Groovy\Thymeleaf\Velocity和Mustache,Spring Boot 中推荐使用Thymeleaf作为模板引擎,因为Thymeleaf提供了完美的Spring MVC的支持。
2、Thymeleaf基础知识
Thymeleaf是一个Java类库,他说一个xml/xhtml/html5的模板引擎,可以作为MVC的web应用的View层。
Thymeleaf还提供了额外的模块与Spring MVC集成,所以我们还可以使用Thymeleaf完全替代JSP。
2.1、引入Thymeleaf
在标签中添加thymeleaf命名空间,将静态页面转换为动态的视图。
通过”@{}”引用web静态资源。
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<script th:src="@{jqeury.min.js}" type="text/javascript"></script>
</body>
</html>
2.2范围model中的数据
通过”${}”访问model中的属性,这个jsp即为相似。
<div>
<h3>访问model</h3>
<span th:text="${singlePerson.name}"></span>
</div>
2.3.引入URL
Thymeleaf对于URL的处理是通过语法@{…}来处理的
<a th:href="@{https://blog.csdn.net/java_mdzy/article/category/7716138}">绝对路径</a>
<a th:href="@{/}">相对路径</a>