1. 模板中使用索引
1. th:eath----迭代方法
作用:遍历 , 类似于 c:forEach
实例:
<!-- ${usernArrayList}是后端传过来的值 -->
<tr th:each="list, listStat: ${usernArrayList}" >
<td th:text="${list.activityTeamName}"></td>
<td th:text="${list.officialActivityOrderDetail}"></td>
<td th:text="${list.customerName}"></td>
<td th:text="${list.customerGender}==0?'女':'男'"></td>
<td th:text="${list.customerChildName}"></td>
<td th:text="${list.customerChildGender}==0?'女':'男'"></td>
<td th:text="${list.customerChildBirthday}"></td>
<td th:text="${list.customerCity}"></td>
<td th:text="${listStat.index}" ></td> <!-- 这里会输出他的索引(0,1,2)之类的数据 -->
</tr>
扩展:获取迭代中的索引和其他相应的值(注意:上面的 usernListStat ,我们是通过它来获取索引之类的值的)
状态变量定义在一个th:每个属性和包含以下数据:
1.当前迭代索引,从0开始。这是索引属性。index
2.当前迭代索引,从1开始。这是统计属性。count
3.元素的总量迭代变量。这是大小属性。 size
4.iter变量为每个迭代。这是目前的财产。 current
5.是否当前迭代是奇数还是偶数。这些even/odd的布尔属性。
6.是否第一个当前迭代。这是first布尔属性。
7.是否最后一个当前迭代。这是last布尔属性
2. th:attr标签----修改任意属性
作用:当后端传入参数,我们需要使用thymeleaf标签获取值,而有些标签thymeleaf没有定制,所以这时候就需要用到它了
使用方法:
使用前:(注意:因为不是th标签,所以这样是获取不到传入的值的)
<div data-target=" ${list.index} "></div>
使用后:(这样才能正确的获取值)
<div th:attr= "data-target= ${list.index} "></div>
更多使用实例:
<div id = "${list.index} "></div><!-- 前 -->
<div th:attr= "id = ${list.index} "></div><!-- 后 -->
注意:
如果我们需要修改标签的属性,其实可以直接在标签前面加 th :就可以了(就算没有提示有这个标签,也能获取${}中的值)
<div th:data-target=" ${list.index} "></div>
3. 拼接字符串
作用:当我们标签中的数据需要 动态值 + 固定值 的时候
需要的结果 :data-target= " #myModal0 " ( 这里假设${list.index}获取到的值为0)
<div data-target="'#myModal' + ${list.index}"></div>
拼接:
<div th:attr= "data-target='#myModal' + ${list.index}"></div>
4. 如何引用静态资源(扩展springboot知识)
注意:因为这样的引用方式是使用了相对路径,所以注意 ‘../
’
模板引用:方式相同,只是多几个字母
<script src="../js/jquery-3.3.1.min.js"></script>
<script th:src="../js/jquery-3.3.1.min.js"></script>