1、spring boot 集成 thymeleaf
在pom.xml 文件中添加:
<!--引入thymeleaf的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
项目目录结构
x
2、thymeleaf 常用标签
一:th:each
<option th:each="zjCategory:${zjCategoryDOs}" th:value="${zjCategory.id}" th:text="${zjCategory.name}"></option>
二:th:if (如果不满足条件,该标签整个都不会显示)
<span th:if="${zjReply.delFlag==1}" th:text="有效" class="label label-primary"></span>
<span th:if="${zjReply.delFlag==0}" th:text="无效" class="label label-warning"></span>
三:th:onclick(点击事件)
<a th:onclick="${'updateDelFlag('+zjReplyChilds.id+',0)'}">下架</a>
<a th:onclick="${'updateDelFlag('+zjReplyChilds.id+',1)'}">上架</a>
四:th:href (语法:href="@{'/aa/${mode属性}'}")
<a th:href="@{'/zjcomment-q'+${queryType}+'-c-o-t.html'}">全部</a>
五:三元运算符 弥补 th:if 的 不足
作用:
在thymeleaf 中 没有
if(条件){
}
所以可以用 三元运算符。
语法:${ a==1 ?'等于':'不等于'}
例子: th:class="${#strings.isEmpty(category1)}?'active':''" 【#strings.isEmpty(category1) 判断category1 是否为"" 或者null】
六:th:block 运算块
可以让thymeleaf 不用写在元素上
七:th:include 包含代码块
<footer class="mt_40" th:include="zjcomment/page::footer"></footer>
【其中zjcomment 是包名 page 是文件名 footer 是代码块中 th:fragment="footer"】
八:th:attr 自定义属性
<a th:attr="data-id=${'c'+zjCategory.id}" ></a>
九:thymeleaf 时间格式的显示
<th:block th:text="${zjcomment.createTime}==null?null:${#dates.format(zjcomment.createTime,'yyyy-MM-dd HH:mm')}"></th:block>
十:th:utext 富文本框内容显示网页中
<th:block th:utext="${zjcomment.context}"></th:block>