做完了一个项目,把全部用到的thymeleaf的用法总结记录一下,反正记是肯定记不住的,做个笔记方便以后查阅吧。
0.数据来源
在java后端添加到model中,根据键值对的键,在html文件里面的读取。
1.th:text
th:text="${question}"
<!--字符串与变量拼接-->
th:text="'截止时间:'+${date}"
2.th:if
th:if="${var1 != var2}"
th:if="${user.type == 1}"
3.th:each
<!-- 遍历users(list) -->
th:each="user : ${users}"
4.三元运算符
th:text="${user.type==1?'管理员':'标注工人'}"
5.th:onclick
th:onclick="deleteUser([[${user.id}]]);"
th:onClick="|working(${projectId},${index+1},${taskId})|"
6.th:href
th:href="@{'/downloadExcel/'+${project.id}+'/'+${project.name}}"
th:href="@{/(page=${pageDTO.page+1})}"
7.th:style
th:style=" 'width:' + @{${project.progress}} +'%; height: 6px;' " th:aria-valuenow="${project.finishedCount}"
8.处理时间格式
th:text="${#dates.format(project.gmtCreate,'yyyy-MM-dd')}"
th:text="${#dates.format(project.deadline,'yyyy-MM-dd HH:mm')}"
9.th:class
th:class="'page-item ' + @{${pageDTO.page==page}?'active':''}"