Skip to content

thymeleaf

逻辑判断(&&,||)

html
<span th:if="${sex != 1 && age eq 4}">判断是否相等,且比较大小</span>

内外链接符号(+)

html
<td th:title="${task.taskName} + '-链接符号附加内容'" th:text="${task.taskName + '-链接符号附加内容' }"></td>

List 非空判断

th:if="${fileList!=null and !fileList.empty}"

Object 非空判断

th:value=${subcontractorInfo?.Subcontractor_Address}

三目法则

th:value="${subcontractorInfo.Date_of_Established} != null ? ${subcontractorInfo.Date_of_Established} : '2005' "

判空(null, ‘’)判断对象、属性字段是不是null:

html
<input type="radio" name="isExist" value="1" checked="checked" th:disabled="${null != info && null != info.isExist && info.isExist != ''}"> 存在问题

条件

switch

html
<th:block th:switch="${itemStat.index}">
      <p th:case="1">User is an administrator</p> 值为1情况
      <p th:case="*">User is a manager</p> 其他情况
</th:block>
html
条件判断通常用于动态页面的初始化,例如:
<div th:if="${users}">
    <div>哈哈哈,users存在</div>
</div>
如果想取反则使用unless 例如
<div th:unless="${userList}">
    <div>不存在..</div>
</div>

<div th:if="${news.listViewCode} =='NO_IMG'" class="l-tongyong box-sha mb28 clearfix">
    <!-- 无图新闻 -->
    <a class="l-tit e2" th:href="${news.article.pcUrl}">[(${news.listTitle})]</a>
    <p class="l-inrto clearfix"><span class="l-name">[(${news.article.tag})]</span><span class="l-ml">|</span><span
            class="l-time">[(${news.pubTime})]</span> <span class="l-share">
            <span class="l-share-box">
                <a target="_self" class="weixin" href="javascript:void()"
                    onClick="javascript:shareTo(this,'weixin',0);"><span>微信</span></a>
                <a target="_self" class="tsina" href="javascript:void()"
                    onClick="javascript:shareTo(this,'sinaminiblog',0);"><span>微博</span></a>
            </span>
        </span></p>
</div>

//判断取值
<a th:target="${param1.columnsType}==0 ? '_self':'_blank'">columnsType 语法测试</a>

strings

html
字符串长度判断
<p th:if="${item.length>5}"></p>
调用参数的toString方法返回字符串
<div th:text="${#strings.toString('hello')}"></div>
返回字符串的长度
<div th:text="${#strings.length('hello')}"></div>
判断是否为空或null
<div th:text="${#strings.isEmpty('hello')}"></div>
<div th:text="${#strings.isEmpty('')}"></div>
<div th:text="${#strings.isEmpty(null)}"></div>
为空或null时设置默认值
<div th:text="${#strings.defaultString('hello','a')}"></div>
<div th:text="${#strings.defaultString('','b')}"></div>
<div th:text="${#strings.defaultString(null,'c')}"></div>
判断是否包含(区分大小写)
<div th:text="${#strings.contains('hello','he')}"></div>
<div th:text="${#strings.contains('hello','HE')}"></div>
判断是否包含(忽略大小写)
<div th:text="${#strings.containsIgnoreCase('hello','he')}"></div>
<div th:text="${#strings.containsIgnoreCase('hello','HE')}"></div>
判断开头和结尾是否包含(区分大小写)
<div th:text="${#strings.startsWith('hello','he')}"></div>
<div th:text="${#strings.startsWith('hello','HE')}"></div>
<div th:text="${#strings.startsWith('hello','el')}"></div>
<div th:text="${#strings.endsWith('hello','lo')}"></div>
获取字符串的索引(如果不存在返回-1)
<div th:text="${#strings.indexOf('hello','el')}"></div>
<div th:text="${#strings.indexOf('hello','ee')}"></div>
指定开始和结束索引,截取字符串(如果索引超过字符串长度,则抛出异常)
<div th:text="${#strings.substring('hello',1,3)}"></div>
指定从某个字符串后面截取字符串(如果不包含则返回空字符串)
<div th:text="${#strings.substringAfter('hello','e')}"></div>
<div th:text="${#strings.substringAfter('hello','ee')}"></div>
指定从某个字符串前面截取字符串(如果不包含则返回空字符串)
<div th:text="${#strings.substringBefore('hello','e')}"></div>
<div th:text="${#strings.substringBefore('hello','ee')}"></div>
替换字符串
<div th:text="${#strings.replace('hello','e','a')}"></div>
去除html字符串
<div th:text="$ {#strings.replace (htmlContent,’(?s)<p. *?〉く/p〉’,’’)}"></div>
转换为大写
<div th:text="${#strings.toUpperCase('hello')}"></div>
转换为小写
<div th:text="${#strings.toLowerCase('HELLO')}"></div>
首字母转换为大写
<div th:text="${#strings.capitalize('hello')}"></div>
首字母转换为小写
<div th:text="${#strings.unCapitalize('heLLo')}"></div>
每个单词的首字母转为大写
<div th:text="${#strings.capitalizeWords('hello world')}"></div>
根据分隔符将每个单词的首字母转换为大写
<div th:text="${#strings.capitalizeWords('hello-world','-')}"></div>
字符串前面追加
<div th:text="${#strings.prepend('world','hello ')}"></div>
字符串后面追加
<div th:text="${#strings.append('hello',' world')}"></div>
拼接字符串(参数个数不限)
<div th:text="${#strings.concat('hello',' world',' !')}"></div>
从第二个参数之后拼接字符串,如果参数为null,则用第一个参数替代
<div th:text="${#strings.concatReplaceNulls('*','hello',null,'world')}"></div>
删除空白
<div th:text="${#strings.trim(' hello ')}"></div>
字符串截取指定长度(最小为3),后面加...
<div th:text="${#strings.abbreviate('hello,world', 8)}"></div>
产生指定位数的随机字母数字,范围为大写英文字母加0-9数字
<div th:text="${#strings.randomAlphanumeric(4)}"></div>
调用HtmlEscape类的escapeHtml4Xml方法对参数进行编码
<div th:text="${#strings.escapeXml('<span>hello</span>')}"></div>

dates

html
时间格式化操作(#data.format(createTime, ‘yyyy-MM-dd HH:mm’))
<td th:text="${#dates.format(createTime, 'yyyy-MM-dd HH:mm')}"></td>
| ${#dates.format(key)} | 格式化日期,默认的以浏览器默认语言为格式化标准 |
| ${#dates.format(key,‘yyy/MM/dd’)} | 按照自定义的格式做日期转换 |
| ${#dates.year(key)} | 取年 |
| ${#dates.month(key)} | 取月 |
| ${#dates.day(key)} | 取日 |