简介
thymeleaf是用来代替jsp的模板引擎
可以用th:表达式从后台获取数据
基本语法
th:text
用来渲染一个文本
<div th:text="${msg}">request设置的msg值</div>
<div th:text="${session.msg}">session设置的msg</div>
<div th:text="${application.msg}">application的msg</div>
th:object
用来渲染一个对象,里面用直接使用***或者$**或者取出对象的属性
<div th:object="${book}">
<span th:text="*{name}"></span>
<span th:text="*{price}"></span>
</div>
也用点表示从对象中取出属性
<div th:text="${book.name}">
要向获取一个对象的toString,还是得用th:text
<div th:text="${book}">
th:if
表示要不要显示一个其中的元素(或者文本)
<span th:if="${user.id==1}">
是第一个用户
</span>
th:each
循环遍历取出数组或者映射中的元素(注意用的是冒号不是in关键字)
list列表
<span th:each="obj,stat: ${list}">
列表值: <span th:text="${obj}"></span>
索引: <span th:text="${stat.index}"></span><br/>
</span>
map映射(用key和value取出键和值)
<div th:each="entry: ${book_map}">
<span th:text="${entry.key}"></span>
<span th:text="${entry.value}"></span>
</div>
th:switch和th:case
<!--switch语句-->
<span th:switch="${user.id}">
<span th:case="1">id是1</span>
<span th:case="2">id是2</span>
<span th:case="3">id是3</span>
</span>
th:href
一条省略了上下文的超链接, 只能是url不能是本地地址
<a th:href="@{/h1}">转到h1</a>