thymeleaf常用标签

时间:2021-07-22 15:01:42

1. th:checked ,th:selected标签
<input type="radio" value="M" name="gender" th:checked="${data.gender}=='M'"/>男
<input type="radio" value="F" name="gender" th:checked="${data.gender}=='F'"/>女
<option th:selected="${cookie[draftStatus]}?${cookie[draftStatus].getValue()}=='WSH':false">未审核</option>

2. 日期格式化,字符串截取
<span th:text="${#calendars.format(customerInfoVO.gmtCreated,'yyyy-MM-dd HH mm')}"></span>
<span th:text="${#strings.substring(entity.contactTime,0,10)}"></span>
tempo 写法 <span>{{gmtCreate | date 'YYYY-MM-DD HH:mm'}} </span>

3. a 标签带参数
<a th:if="(${entity.draftStatus}=='审核不通过') or (${entity.draftStatus}=='已发布') " class="btn btn-info" th:href="@{getItemById?(id=${entity.id},draftStatus=${entity.draftStatus}) }">编辑</a>
th:href="@{/index/bannerList}" 加‘/’ url 取相对路径 (http://localhost:8081/bauna)

4.th:attr
th:attr="itemId='DQR'+${entity.id},'src'=${entity.itemImg}"

5.列表序号 从1开始
<th:block th:each="entity,itemStat : ${contactNotes.getContactNotes()}">
<tr class="alignCenter entryOrderRow">
<td th:text="${itemStat.count+((page.curPage-1)*page.pageSize)}"></td>
<td th:text="${#calendars.format(entity.gmtCreate,'yyyy-MM-dd')}"></td>
</tr>
</th:block>

6.判断条件 表达式
<span th:if="1==1"></span> thymeleaf
{% if changeAmountTimes == 0 %} tempo模板
1111
{% else %}
22222
{% endif %}

7. 三元表达式
<td th:text="${entity.contactTimeNext!=null?(#strings.substring(entity.contactTimeNext,0,10)):' '}"></td>

8.tempo 渲染
<tr data-template-for="userRoleOrgList">
<td colspan="2">部门:<span>{{orgName}}</span></td>
<td colspan="10">角色:<span>{{roleDescription}}</span></td>
</tr>
userRoleOrgList为user 对象属性时 循环userRoleOrgList

9.字符串处理
${#strings.length(entity.content)} 获取字符串长度
${#strings.substring(entity.content,0,20)} 截取

10.页面数据供js 调用
<script th:inline="javascript">
/*<![CDATA[*/
var goodsList = [[${goodsList}]];
/*]]>*/
</script>

11.th:class, th:classappend, th:colspan
<tr th:classappend="(${entity.isContact}=='Y')?a:b" class="row-body alignCenter">
如果${entity.isContact}=='Y' tr 采用样式a , 否则 用样式 b
<td th:colspan="${orderEntity.invoiceStatus}!=null?2:3"></td>

12.转义标签 th:utext
<span style="color:#41a1d2;" >2016-06-17 16:43</span>
tempo 转义 :Tempo.prepare('orderTempo',{'escape': false})

13.工具类
日期处理 ${#dates.format(date,'dd/MMM/yyyy HH:mm')}
${#calendars.format(cal,'dd/MMM/yyyy HH:mm')}
数字 ${#numbers.formatInteger(1000000,3,'COMMA')}
字符串 ${#strings.contains(name,'EZ')}
$(#strings.substring(name,3,5))
集合 ${#arrays.length(array)}
${#lists.size(list)}