thymeleaf模板部分知识(持续更新)

来源:互联网 发布:地暖好吗知乎 编辑:程序博客网 时间:2024/06/15 19:03

由于视图层一直用的是jsp,在初次接触模板时难免遇到一些不为人知的知识点,通过查文档,google,请教前辈,可以慢慢积累一些知识点,在此记录,以方便后来人查阅,也进一步巩固这些知识点。


1)a标签href属性的路径带多参数,还有一种拼接,没有这个清爽

<a th:href="@{/manage/permission/update(id=${p.id},page=${page})}" th:text="${p.description}"></a>

2)if else判断

<span th:if="${level.isUse==1}" th:text="已使用"></span><span th:unless="${level.isUse==1}" th:text="未使用"></span>

3)在js代码块中获得Model中存储的数据
var name = '[[${name}]]';

4)格式化日期,此模板已经封装好的

<td th:text="${#calendars.format(admin.createTime,'yyyy-MM-dd HH:mm:ss')}"></td><span th:text="${#dates.format(admin.createTime, 'yyyy-MM-dd HH:mm:ss')}"></span>


5)添加点击事件,调用js函数

<a href="#editModal" th:onclick="'javascript:edit('+${user.id}+');'">修改</a>

6)thymeleaf中使用三元运算符

<span th:text="${sex eq 0 ? '女' : '男'}"></span>

7)获取list长度

<span th:text="${#lists.size(users)}"></span>

8)数字格式化

<span th:text="${#numbers.formatDecimal(price, 0, 2)}"></span><!--保留两位小数,整数位自动--><span th:text="${#numbers.formatDecimal(money, 3, 2)}"></span><!--保留两位小数,3位整数位-->

9)thymeleaf中循环纯数字

<th:block th:each="i:${#numbers.sequence(1,5)}">        <h3 th:text="${i}"></h3></th:block>


10)引用公共页面,在要引用的代码块上使用th:fragment做标记,在主页面使用th:include引用

要引用的代码块

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"><div th:fragment="footer"><span class="footer-brand"><strong class="text-danger">日报</strong> 后台管理</span><p class="no-margin">&copy; 2017 <strong>宁波日报</strong>. 版权所有.</p></div></html>
主页面,下面标签中第一个footer是模板名,第二个是标记名

<footer class="footer" th:include="footer :: footer">        <!-- 引用底部 --></footer>




0 0
原创粉丝点击