9)Thymeleaf ⼯具类对象表达式
目录
#execInfo
URIs/URLs
Dates 日期工具类
Calendars 日期工具类
Numbers 数字工具类
Strings
Objects
Booleans
Arrays
Lists
Sets
Maps
Aggregates 聚合函数
IDs
#execInfo
#execInfo:提供有关在 Thymeleaf 标准表达式内正在处理的模板的信息。
//See javadoc API for class org.thymeleaf.expression.ExecutionInfo
<body>
<p>#execInfo.templateName=[[${#execInfo.templateName}]]</p>
<p>#execInfo.templateMode=[[${#execInfo.templateMode}]]</p>
<p>#execInfo.processedTemplateName=[[${#execInfo.processedTemplateName}]]</p>
<p>#execInfo.processedTemplateMode=[[${#execInfo.processedTemplateMode}]]</p>
<p>#execInfo.templateNames=[[${#execInfo.templateNames}]]</p>
<p>#execInfo.templateModes=[[${#execInfo.templateModes}]]</p>
<p>#execInfo.templateStack=[[${#execInfo.templateStack}]]</p>
</body>
URIs/URLs
#uris:⽤于在 Thymeleaf 标准表达式中执⾏ URI / URL 操作(尤其是转义/取消转义)的⼯具对象。
// See javadoc API for class org.thymeleaf.expression.Uris
Escape/Unescape as a URI/URL path(对 URI、URL 转码)
${#uris.escapePath(uri)} //转码
${#uris.escapePath(uri, encoding)} //指定编码转码${#uris.unescapePath(uri)} //解码
${#uris.unescapePath(uri, encoding)} //指定编码解码
<!--设置局部变量 url,用于操作转码-->
<body th:with="url='http://localhost/thymeleaf/user/home?u_id=9527&name=东方不败'"><!--将转码结果存储为局部变量,方便输出和解码-->
<div th:with="escapePath=${#uris.escapePath(url)}"><p>被转码 url:[[${url}]]</p><p>#uris.escapePath(uri) 转码后:[[${escapePath}]]</p><p>#uris.unescapePath(uri) 解码后:[[${#uris.unescapePath(escapePath)}]]</p>
</div>
</body>
类似的还有:
${#uris.escapePathSegment(uri)}
${#uris.escapePathSegment(uri, encoding)}
${#uris.unescapePathSegment(uri)}
${#uris.unescapePathSegment(uri, encoding)}${#uris.escapeFragmentId(uri)}
${#uris.escapeFragmentId(uri, encoding)}
${#uris.unescapeFragmentId(uri)}
${#uris.unescapeFragmentId(uri, encoding)}${#uris.escapeQueryParam(uri)}
${#uris.escapeQueryParam(uri, encoding)}
${#uris.unescapeQueryParam(uri)}
${#uris.unescapeQueryParam(uri, encoding)}
Dates 日期工具类
#dates:java.util.Date 对象的实⽤程序⽅法。//See javadoc API for class org.thymeleaf.expression.Dates
从后台输出的数据中如果含有日期,则需要进行格式化。
Format date with the standard locale format ,Also works with arrays, lists or sets
(使用标准区域设置格式设置日期,也可以是日期数组、列表或集合————实际中这些用的少)
${#dates.format(date)}
${#dates.arrayFormat(datesArray)}
${#dates.listFormat(datesList)}
${#dates.setFormat(datesSet)}
Format date with the ISO8601 format, Also works with arrays, lists or sets
(使用ISO8601格式格式化日期,也可以使用数组、列表或集合—————实际中这些用的少)
${#dates.formatISO(date)}
${#dates.arrayFormatISO(datesArray)}
${#dates.listFormatISO(datesList)}
${#dates.setFormatISO(datesSet)}
Format date with the specified pattern, Also works with arrays, lists or sets
(用指定的模式格式化日期,也可以使用数组、列表或集合—————实际中通常都是用它)
${#dates.format(date, 'dd/MMM/yyyy HH:mm')}
${#dates.arrayFormat(datesArray, 'dd/MMM/yyyy HH:mm')}
${#dates.listFormat(datesList, 'dd/MMM/yyyy HH:mm')}
${#dates.setFormat(datesSet, 'dd/MMM/yyyy HH:mm')}
<body>
<!--/*后台输出:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse("1993-08-25 18:25:12");
model.addAttribute("birthday", date);
*/-->
<p th:text="'原日期:'+${birthday}"></p>
<p th:text="'yyyy-MM-dd HH:mm:ss:'+${#dates.format(birthday,'yyyy-MM-dd HH:mm:ss')}"></p>
<p th:text="'yyyy-MM-dd hh:mm:ss:'+${#dates.format(birthday,'yyyy-MM-dd hh:mm:ss')}"></p>
<p th:text="'yyyy-MM-dd HH:mm:'+${#dates.format(birthday,'yyyy-MM-dd HH:mm')}"></p>
<p th:text="'yyyy-MM-dd HH:'+${#dates.format(birthday,'yyyy-MM-dd HH')}"></p>
<p th:text="'yyyy-MM-dd:'+${#dates.format(birthday,'yyyy-MM-dd')}"></p>
<p th:text="'yyyy/MM/dd HH:mm:'+${#dates.format(birthday,'yyyy/MM/dd HH:mm')}"></p>
</body>
更多内容可参考官网:https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#dates
Calendars 日期工具类
#calendars:类似于 #dates,但对于 java.util.Calendar 对象,See javadoc API for class org.thymeleaf.expression.Calendars
可参考官网:https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#calendars
<body>
<!--/*后台输出:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse("1993-08-25 18:25:12");
model.addAttribute("birthday", date);
*/-->
<p th:text="'原日期:'+${birthday}"></p>
<p th:text="'yyyy-MM-dd HH:mm:ss:'+${#calendars.format(birthday,'yyyy-MM-dd HH:mm:ss')}"></p>
<p th:text="'yyyy-MM-dd hh:mm:ss:'+${#calendars.format(birthday,'yyyy-MM-dd hh:mm:ss')}"></p>
<p th:text="'yyyy-MM-dd HH:mm:'+${#calendars.format(birthday,'yyyy-MM-dd HH:mm')}"></p>
<p th:text="'yyyy-MM-dd HH:'+${#calendars.format(birthday,'yyyy-MM-dd HH')}"></p>
<p th:text="'yyyy-MM-dd:'+${#calendars.format(birthday,'yyyy-MM-dd')}"></p>
<p th:text="'yyyy/MM/dd HH:mm:'+${#calendars.format(birthday,'yyyy/MM/dd HH:mm')}"></p>
</body>
结果与 #datas 完全一样。
Numbers 数字工具类
#numbers:数字对象的实⽤程序⽅法,See javadoc API for class org.thymeleaf.expression.Numbers
用于格式化数字。
Set minimum integer digits. Also works with arrays, lists or sets
(设置最小整数位数。也适用于数组、列表或集合)
格式:${#numbers.formatInteger(num,size)}:num 表示被格式的数字,size 表示整数位最少保留几位。
<body>
<!--/*后台输出:model.addAttribute("age", 35);*/-->
<p th:text="'原数字:'+${age}"></p>
<p th:text="'0:'+${#numbers.formatInteger(age,0)}"></p>
<p th:text="'1:'+${#numbers.formatInteger(age,1)}"></p>
<p th:text="'2:'+${#numbers.formatInteger(age,2)}"></p>
<p th:text="'3:'+${#numbers.formatInteger(age,3)}"></p>
<p th:text="'4:'+${#numbers.formatInteger(age,4)}"></p>
</body>
Set minimum integer digits and thousands separator: 'POINT', 'COMMA', 'WHITESPACE', 'NONE' or 'DEFAULT' (bylocale). Also works with arrays, lists or sets
(设置最小整数位数和数以千计的分隔符:“点”、“逗号”、“空白”、“没有”或“默认”(字节环境)。也适用于数组、列表或集合)
格式:${#numbers.formatInteger(num,size,format)}:num 表示被格式的数字,size 表示整数位最少保留几位,format 表示格式,有: 'POINT', 'COMMA', 'WHITESPACE', 'NONE' or 'DEFAULT'
<body th:with="price=32008822">
<p th:text="'原数字:'+${price}"></p>
<p th:text="'POINT:'+${#numbers.formatInteger(price,0,'POINT')}"></p>
<p th:text="'COMMA:'+${#numbers.formatInteger(price,0,'COMMA')}"></p>
<p th:text="'WHITESPACE:'+${#numbers.formatInteger(price,0,'WHITESPACE')}"></p>
<p th:text="'NONE:'+${#numbers.formatInteger(price,0,'NONE')}"></p>
<p th:text="'DEFAULT:'+${#numbers.formatInteger(price,0,'DEFAULT')}"></p>
</body>
更多用法请参考官网:https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#numbers
/** ==========================* Formatting decimal numbers* ==========================*//** Set minimum integer digits and (exact) decimal digits.* Also works with arrays, lists or sets*/
${#numbers.formatDecimal(num,3,2)}
${#numbers.arrayFormatDecimal(numArray,3,2)}
${#numbers.listFormatDecimal(numList,3,2)}
${#numbers.setFormatDecimal(numSet,3,2)}/** Set minimum integer digits and (exact) decimal digits, and also decimal separator.* Also works with arrays, lists or sets*/
${#numbers.formatDecimal(num,3,2,'COMMA')}
${#numbers.arrayFormatDecimal(numArray,3,2,'COMMA')}
${#numbers.listFormatDecimal(numList,3,2,'COMMA')}
${#numbers.setFormatDecimal(numSet,3,2,'COMMA')}/** Set minimum integer digits and (exact) decimal digits, and also thousands and * decimal separator.* Also works with arrays, lists or sets*/
${#numbers.formatDecimal(num,3,'POINT',2,'COMMA')}
${#numbers.arrayFormatDecimal(numArray,3,'POINT',2,'COMMA')}
${#numbers.listFormatDecimal(numList,3,'POINT',2,'COMMA')}
${#numbers.setFormatDecimal(numSet,3,'POINT',2,'COMMA')}/** ==========================* Utility methods* ==========================*//** Create a sequence (array) of integer numbers going* from x to y*/
${#numbers.sequence(from,to)}
${#numbers.sequence(from,to,step)}
Strings
#strings String ⼯具类,就是字符串工具类,这以前 JSP 中的 JSTL 也有这种功能,如下所示,从方法名称即可猜到用途。
官网地址:https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#strings
/** ======================================================================* See javadoc API for class org.thymeleaf.expression.Strings* ======================================================================*//** Null-safe toString()*/
${#strings.toString(obj)} // also array*, list* and set*/** Check whether a String is empty (or null). Performs a trim() operation before check* Also works with arrays, lists or sets*/
${#strings.isEmpty(name)}
${#strings.arrayIsEmpty(nameArr)}
${#strings.listIsEmpty(nameList)}
${#strings.setIsEmpty(nameSet)}/** Perform an 'isEmpty()' check on a string and return it if false, defaulting to* another specified string if true.* Also works with arrays, lists or sets*/
${#strings.defaultString(text,default)}
${#strings.arrayDefaultString(textArr,default)}
${#strings.listDefaultString(textList,default)}
${#strings.setDefaultString(textSet,default)}/** Check whether a fragment is contained in a String* Also works with arrays, lists or sets*/
${#strings.contains(name,'ez')} // also array*, list* and set*
${#strings.containsIgnoreCase(name,'ez')} // also array*, list* and set*/** Check whether a String starts or ends with a fragment* Also works with arrays, lists or sets*/
${#strings.startsWith(name,'Don')} // also array*, list* and set*
${#strings.endsWith(name,endingFragment)} // also array*, list* and set*/** Substring-related operations* Also works with arrays, lists or sets*/
${#strings.indexOf(name,frag)} // also array*, list* and set*
${#strings.substring(name,3,5)} // also array*, list* and set*
${#strings.substringAfter(name,prefix)} // also array*, list* and set*
${#strings.substringBefore(name,suffix)} // also array*, list* and set*
${#strings.replace(name,'las','ler')} // also array*, list* and set*/** Append and prepend* Also works with arrays, lists or sets*/
${#strings.prepend(str,prefix)} // also array*, list* and set*
${#strings.append(str,suffix)} // also array*, list* and set*/** Change case* Also works with arrays, lists or sets*/
${#strings.toUpperCase(name)} // also array*, list* and set*
${#strings.toLowerCase(name)} // also array*, list* and set*/** Split and join*/
${#strings.arrayJoin(namesArray,',')}
${#strings.listJoin(namesList,',')}
${#strings.setJoin(namesSet,',')}
${#strings.arraySplit(namesStr,',')} // returns String[]
${#strings.listSplit(namesStr,',')} // returns List<String>
${#strings.setSplit(namesStr,',')} // returns Set<String>/** Trim* Also works with arrays, lists or sets*/
${#strings.trim(str)} // also array*, list* and set*/** Compute length* Also works with arrays, lists or sets*/
${#strings.length(str)} // also array*, list* and set*/** Abbreviate text making it have a maximum size of n. If text is bigger, it* will be clipped and finished in "..."* Also works with arrays, lists or sets*/
${#strings.abbreviate(str,10)} // also array*, list* and set*/** Convert the first character to upper-case (and vice-versa)*/
${#strings.capitalize(str)} // also array*, list* and set*
${#strings.unCapitalize(str)} // also array*, list* and set*/** Convert the first character of every word to upper-case*/
${#strings.capitalizeWords(str)} // also array*, list* and set*
${#strings.capitalizeWords(str,delimiters)} // also array*, list* and set*/** Escape the string*/
${#strings.escapeXml(str)} // also array*, list* and set*
${#strings.escapeJava(str)} // also array*, list* and set*
${#strings.escapeJavaScript(str)} // also array*, list* and set*
${#strings.unescapeJava(str)} // also array*, list* and set*
${#strings.unescapeJavaScript(str)} // also array*, list* and set*/** Null-safe comparison and concatenation*/
${#strings.equals(first, second)}
${#strings.equalsIgnoreCase(first, second)}
${#strings.concat(values...)}
${#strings.concatReplaceNulls(nullValue, values...)}/** Random*/
${#strings.randomAlphanumeric(count)}
Objects
- #objects : utility methods for objects in general
/** ======================================================================* See javadoc API for class org.thymeleaf.expression.Objects* ======================================================================*//** Return obj if it is not null, and default otherwise* Also works with arrays, lists or sets*/
${#objects.nullSafe(obj,default)}
${#objects.arrayNullSafe(objArray,default)}
${#objects.listNullSafe(objList,default)}
${#objects.setNullSafe(objSet,default)}
Booleans
- #bools : utility methods for boolean evaluation
/** ======================================================================* See javadoc API for class org.thymeleaf.expression.Bools* ======================================================================*//** Evaluate a condition in the same way that it would be evaluated in a th:if tag* (see conditional evaluation chapter afterwards).* Also works with arrays, lists or sets*/
${#bools.isTrue(obj)}
${#bools.arrayIsTrue(objArray)}
${#bools.listIsTrue(objList)}
${#bools.setIsTrue(objSet)}/** Evaluate with negation* Also works with arrays, lists or sets*/
${#bools.isFalse(cond)}
${#bools.arrayIsFalse(condArray)}
${#bools.listIsFalse(condList)}
${#bools.setIsFalse(condSet)}/** Evaluate and apply AND operator* Receive an array, a list or a set as parameter*/
${#bools.arrayAnd(condArray)}
${#bools.listAnd(condList)}
${#bools.setAnd(condSet)}/** Evaluate and apply OR operator* Receive an array, a list or a set as parameter*/
${#bools.arrayOr(condArray)}
${#bools.listOr(condList)}
${#bools.setOr(condSet)}
Arrays
- #arrays : utility methods for arrays
/** ======================================================================* See javadoc API for class org.thymeleaf.expression.Arrays* ======================================================================*//** Converts to array, trying to infer array component class.* Note that if resulting array is empty, or if the elements* of the target object are not all of the same class,* this method will return Object[].*/
${#arrays.toArray(object)}/** Convert to arrays of the specified component class.*/
${#arrays.toStringArray(object)}
${#arrays.toIntegerArray(object)}
${#arrays.toLongArray(object)}
${#arrays.toDoubleArray(object)}
${#arrays.toFloatArray(object)}
${#arrays.toBooleanArray(object)}/** Compute length*/
${#arrays.length(array)}/** Check whether array is empty*/
${#arrays.isEmpty(array)}/** Check if element or elements are contained in array*/
${#arrays.contains(array, element)}
${#arrays.containsAll(array, elements)}
Lists
- #lists : utility methods for lists
/** ======================================================================* See javadoc API for class org.thymeleaf.expression.Lists* ======================================================================*//** Converts to list*/
${#lists.toList(object)}/** Compute size*/
${#lists.size(list)}/** Check whether list is empty*/
${#lists.isEmpty(list)}/** Check if element or elements are contained in list*/
${#lists.contains(list, element)}
${#lists.containsAll(list, elements)}/** Sort a copy of the given list. The members of the list must implement* comparable or you must define a comparator.*/
${#lists.sort(list)}
${#lists.sort(list, comparator)}
Sets
- #sets : utility methods for sets
/** ======================================================================* See javadoc API for class org.thymeleaf.expression.Sets* ======================================================================*//** Converts to set*/
${#sets.toSet(object)}/** Compute size*/
${#sets.size(set)}/** Check whether set is empty*/
${#sets.isEmpty(set)}/** Check if element or elements are contained in set*/
${#sets.contains(set, element)}
${#sets.containsAll(set, elements)}
Maps
- #maps : utility methods for maps
/** ======================================================================* See javadoc API for class org.thymeleaf.expression.Maps* ======================================================================*//** Compute size*/
${#maps.size(map)}/** Check whether map is empty*/
${#maps.isEmpty(map)}/** Check if key/s or value/s are contained in maps*/
${#maps.containsKey(map, key)}
${#maps.containsAllKeys(map, keys)}
${#maps.containsValue(map, value)}
${#maps.containsAllValues(map, value)}
Aggregates 聚合函数
用于求和、求平均值等操作。
- #aggregates : utility methods for creating aggregates on arrays or collections
/** ======================================================================* See javadoc API for class org.thymeleaf.expression.Aggregates* ======================================================================*//** Compute sum. Returns null if array or collection is empty*/
${#aggregates.sum(array)}
${#aggregates.sum(collection)}/** Compute average. Returns null if array or collection is empty*/
${#aggregates.avg(array)}
${#aggregates.avg(collection)}
官网地址:https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#aggregates
IDs
- #ids : utility methods for dealing with
id
attributes that might be repeated (for example, as a result of an iteration).
/** ======================================================================* See javadoc API for class org.thymeleaf.expression.Ids* ======================================================================*//** Normally used in th:id attributes, for appending a counter to the id attribute value* so that it remains unique even when involved in an iteration process.*/
${#ids.seq('someId')}/** Normally used in th:for attributes in <label> tags, so that these labels can refer to Ids* generated by means if the #ids.seq(...) function.** Depending on whether the <label> goes before or after the element with the #ids.seq(...)* function, the "next" (label goes before "seq") or the "prev" function (label goes after * "seq") function should be called.*/
${#ids.next('someId')}
${#ids.prev('someId')}
9)Thymeleaf ⼯具类对象表达式相关推荐
- Kotlin学习笔记 第二章 类与对象 第十二 十三节 对象表达式与对象声明 类型别名
参考链接 Kotlin官方文档 https://kotlinlang.org/docs/home.html 中文网站 https://www.kotlincn.net/docs/reference/p ...
- C#中IEnumerableT.Distinct()将指定实体类对象用Lambda表达式实现多条件去重
背景说明 在EF等ORM框架中需要以List实体类的方式对数据进行大量操作,其中免不了对一些数据进行去重复,而C#中IEnumerable.Distinct()便提供了这一功能.只是对刚开始接触的新人 ...
- python保存类对象_python 存储类对象吗
Python源码读后小结 Python 笔记 前言(还是叫杂记吧) 在python中一切皆对象, python中的对象体系大致包含了"类型对象", "Mapping对象( ...
- [JAVAEE] Thymeleaf 基本语法:常用表达式
Thymeleaf 基本语法 常用表达式 变量表达式 ${ } 使用方法:th:xx = "${ }" 获取对象属性值给 th:xx . 后台代码: Student s=new S ...
- python释放类对象_Python 基本功: 10. 面对对象-类 Class
虽然 Python 可以写函数式编程,但是本质上是一门面对对象编程语言 (object-oriented programming language),简称 oop.面对对象编程是把代码包装成一个对象 ...
- (邓爱萍)类 对象 课本
一.类与对象 对象 对象就是组成现实世界的个体,他们之间存在着错综复杂的关系. 面向对象的分析 一些对象具有相似的特征 一些对象之间有相互作用 把这些对象以及对象之间的关系找出来. 类 把个体归纳为不 ...
- python 类 对象 魔法方法概念+习题
类 对象 类 对象是c++和java中都有的内容,python定义类的简单语法如下: class 类名: -类变量或者方法 Python 的类定义有点像函数定义,都是以冒号:作为类体的开始,以统一缩进 ...
- Kotlin学习笔记(六) 伴生对象 对象表达式
2019独角兽企业重金招聘Python工程师标准>>> 一,伴生对象 1.类似于java中的静态方法static class TestCompanion{//伴生对象使用compan ...
- android对象申明,Kotlin中的对象表达式和对象声明的具体使用
Kotlin的对象表达式与Java中的匿名内部类的主要区别:匿名内部类只能指定一个父类型,但对象表达式可以指定0~N个肤类型. 一.对象表达式 对象表达式的语法格式如下: object [: 0~N个 ...
- python类的定义和创建_Python类对象的创建和使用
通过前面章节的学习,我们已经学会如何定义一个类,但要想使用它,必须创建该类的对象. 创建类对象的过程,又称为类的实例化. 类名(参数) 定义类时,如果没有手动添加 __init__() 构造方法,又或 ...
最新文章
- linux下压缩工具总结与使用(参考私房菜)
- 【机器学习】基于opencv实现目标检测,error LNK2001: unresolved external symbol public: virtual bool CvSVM::train...
- setsockopt , getsoctopt 函数的Level 参数和 name 参数对应表!!!
- 客户机服务器文件更新,服务端数据更新,如何更新客户端缓存
- word文档被锁定无法编辑的解决方法
- 前端 new实例后销毁实例_干货|仪器设备检定校准后的确认(实例)
- 深度学习之dropout
- [转]《编程之道》(很老的一篇文章)
- vuejs中根据用户名生成头像背景色
- 聊一聊DTM子事务屏障功能之SQL Server版
- 云耀服务器 NumPy安装 完整过程
- Solr简介,功能,特性
- js面向对象 —— ES6
- Web系统大规模并发—电商秒杀与抢购
- 【数学建模绘图系列教程】绘图模板总结
- Co-VQA : Answering by Interactive Sub Question Sequence
- 时系列数据分析应用图
- BP神经网络预测模型输入数据表是一个表还是可以多个表
- Excel怎么统计多行多列数据出现的重复次数
- 小米评华为鸿蒙,小米参加华为鸿蒙系统实验?小米高管终于回应,网友评论炸了!...
热门文章
- 怎么单选_听力三个选项都出的单选怎么破?| 附今日听力S1S2及听力原文
- python读取文件路径报invalid_Python 解决OPEN读文件报错 ,路径以及r的问题
- 拓端tecdat|python算法对音频信号处理Sonification :Gauss-Seidel迭代算法
- android 自定义加载圈,Android自定义加载控件实现数据加载动画
- 最大公约数PHP算法,php计算两个整数的最大公约数常用算法小结
- 基于sklearn和keras的数据切分与交叉验证
- Ubuntu-安装-有道词典
- 简单神经网络结构一键可视化
- python基于datetime或time模块分别获取当前时间戳
- 纪念学海生涯的最后一次盲审抽签