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


2. 设置图片src


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


3. 设置input的value值

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



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为空时该标签才会显示



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 中用逗号分隔即可;

猜你喜欢

转载自blog.csdn.net/lxinccode/article/details/79739093