1、Thymeleaf是什么?
Thymeleaf—简单来说就是一个模板引擎
模板引擎:就是用来操作模板的引擎,就是用来渲染模板的,模板就是页面,就是用来生成页面的,其实具体的就是使用户界面与业务数据(内容)分离
2、基本使用
首先需要引入命名空间
xmlns:th="http://www.thymeleaf.org"
几种基本的使用形式:
引入web静态资源或请求一个页面:@{ }
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
<a th:href="@{/emp}"></a>
获取上下文中的数据(类似于EL表达式):${ }
<p th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
取thymeleaf工具中的方法、文字消息表达式:#{ }
<input type="text" name="username" th:placeholder="#{login.username}">
//对应的是login.properties中的国际化
login.username=用户名
提供公共部分如:导航栏、侧边栏:~{ ::}
提取:
<nav th:fragment="topbar"></nav>
使用:
<div th:replace="~{commons/commons::topbar}"></div>
遍历:emp:${emps}
<tr th:each="emp:${emps}" >
<td th:text="${emp.getId()}"></td>
<td th:text="${emp.getName()}"></td>
</tr>
欢迎指正,谢谢!