版权声明:本文为 小异常 原创文章,非商用自由转载-保持署名-注明出处,谢谢! 本文网址:https://blog.csdn.net/sun8112133/article/details/107055378
本篇博客主要讲解 Thymeleaf 中的 字面量、运算符和迭代器。
字面量 简单的说就是可以直接写在表达式中固定的值。
在 Thymeleaf 中也支持一些运算符:算术运算、比较运算、条件运算和无操作运算。
+ 、- 、* 、/ 、%
<p th:text="2013 + 2"></p>>(gt) 、<(lt) 、>=(ge) 、<=(le) 、==(eq) 、!=(ne)
<p th:text="2013 > 2"></p>表达式 ? 值1 : 值2
<p th:text="${age} >= 18 ? '成人' : '未成年'"></p>_(注意 ? 和 : 之前不能有空格)
<!-- 如果没有 name 值,则保留标签内的值 --> <p th:text="${name} ?: _">默认值</p>迭代器 是用来遍历数组或集合中的数据,相当于 Java 中的 foreach 表达式。
状态变量 主要用于跟踪迭代器的状态。
index: 索引;count: 当前数量;size: 总数量;current: 当前对象;even: 该元素是否为奇数;odd: 该元素是否为偶数;first: 是否为第一个元素;last: 是否为最后一个元素。 <body> <ul> <li th:each="book,status : ${books}" th:text="${book.name} + ' index:' + ${status.index} + ' count:' + ${status.count} + ' size:' + ${status.size} + ' current:' + ${status.current} + ' even:' + ${status.even} + ' odd:' + ${status.odd} + ' first:' + ${status.first} + ' last:' + ${status.last} ">Text</li> </ul> </body> 博客中若有不恰当的地方,请您一定要告诉我。前路崎岖,望我们可以互相帮助,并肩前行!