thymeleaf 开发日志

thymeleaf 标签:

<html xmlns:th="http://www.thymeleaf.org">

 

发现所有thymeleaf标签都是写在html标签里面的,

 

 

1. 遍历

th:each="a : ${b}"

 

 

b: 后台传递的对象集合

a : 遍历中临时对象名,代表遍历的每一个对象

 

 

示例: 

 

<div th:each="a:${b}" id="1">
    <span th:text="${a.name}"></span>
</div>

 


该标签加在哪个div或者其他标签,该标签就会遍历,而不是只遍历标签内部的,示例的id为1的标签会出现多个;

 

 

遍历指定次数并加上序号

<img th:each="i,iStat:${#numbers.sequence(1, 5)}"
						th:onclick="'javascript:set_img(\'file_zz'+${iStat.index+1}+'\')'"
						  th:attr="id=zz_img+${iStat.index+1}"
							>

 

遍历5次,iStat.index就是索引,从0开始,th:attr是动态设置id

 

获取列表中的某一个元素

<div th:text="${products[1].id}"></div>

 参考 : http://www.6tie.net/p/879487.html

2. 设置图片src

 

<img src="" th:attr="src=${a.img}">

 

 

3. 设置标签值

input

<input type="hidden" id="aft_re" value="0" th:attr="value=${afte}"/>

a,span,textarea

<a class="dropdown-item" href="#" th:text="${name}"></a>

 

 

4. 设置a标签的href

 

<a href="javascript:;"  th:href="@{'/if_1003/infdls_30007/'+${h.id}}"></a>

 

 

5. 设置显示html代码

 

<span  th:utext="${a.content}"></span>

 


这样改标签内的内容就会当成html代码解析

 

 

 

6. 时间格式化

 

<span th:text="${examineTime!=null}  ? ${#dates.format(examineTime,'MM-dd')}"></span>



examineTime : 是后台传递的Date对象

 

 

7. 获取session的值

 

<span th:text="${session.name}"></span>

 

获取session中名为name的值

8. if else

 

<span th:if="${name == null}"></span>

 


当 name 不为空时该标签才会显示

 

<span th:unless="${name == null}">  您好</span>


当name为空时该标签才会显示

 

参考:  https://blog.csdn.net/mygzs/article/details/52527480 ,  http://www.mamicode.com/info-detail-2206878.html

 

9. switch

<div th:switch="${user.role}">

<p th:case="'admin'">User is an administrator</p>
<p th:case="#{roles.manager}">User is a manager</p>
<p th:case="*">User is some other thing</p>
</div>

 

 

 

 


如果值都不在上述case里,则th:case=”*”语句会生效。

 

10. 判断集合是否为空

不为空:

 

<div class="hot_comments" th:unless="${#lists.isEmpty(data.h)}"></div>

 

为空:

 

<div class="hot_comments" th:if="${#lists.isEmpty(data.h)}"></div>

 

11. onclick事件

th:onclick="'javascript:openBox(\''+${curCabNo}+'\',\''+${box.no}+'\')'"

参考 : thymeleaf下设置onclick属性

 

12. 判断是否相等,如果传的是字符串,html中应加单引号,如下

Controller

request.getSession().setAttribute("lv_img", "1");

 

html 

 

 

 th:if="${session.lv_img == '1'}"

13. 获取model中的list中的第n个对象

获取名为data的list中第一个对象的id

th:text="${data.get(0).id}"

判断list的长度大于1

th:if="${data.size() > 1}"

14. 动态设置id和src

<img th:each="i,iStat:${data.c}"
						th:onclick="'javascript:set_img(\'file_zz'+${iStat.index}+'\')'"
						  th:attr="src=${i.img},id=zz_img+${iStat.index}"
							src="/ejs/images/add.jpg">

th:attr 中用逗号分隔即可;

 

15. thymeleaf获取当前时间并格式化输出

https://www.form1.cn/java-java-187.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值