Thymeleaf内置对象表达式大全

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Thymeleaf内置对象表达式大全相关的知识,希望对你有一定的参考价值。

这里整理的是Thymeleaf3.0的版本中的内置表达式
  参考自Thymeleaf3.0官方文档,第18、19条

1.表达式基本对象

参考自Thymeleaf3.0官方文档第18条

1.1 基础对象

#ctx:上下文对象

    ${#ctx.locale}
    ${#ctx.variableNames}
    ${#ctx.request}
    ${#ctx.response}
    ${#ctx.session}
    ${#ctx.servletContext}

#locale:直接访问java.util.Locale与当前请求关联的内容

    ${#locale}

1.2 会话

param:用于检索请求参数
  ${param.foo}是String[]带有foorequest参数值的,因此${param.foo[0]}通常用于获取第一个值

    ${param.foo}              // 检索具有请求参数‘foo‘值的字符串[]
    ${param.size()}
    ${param.isEmpty()}
    ${param.containsKey(‘foo‘)}

session:用于检索会话属性

    ${session.foo}                 // 检索会话属性“foo”
    ${session.size()}
    ${session.isEmpty()}
    ${session.containsKey(‘foo‘)}

application:用于检索应用程序/ servlet上下文属性

    ${application.foo}              // 检索ServletContext属性“foo”
    ${application.size()}
    ${application.isEmpty()}
    ${application.containsKey(‘foo‘)}
请注意,无需指定用于访问请求属性的名称空间(与request参数相对),因为所有请求属性都作为变量自动添加到上下文根目录中:
    ${myRequestAttribute}

1.3 Web上下文对象

#request:直接访问javax.servlet.http.HttpServletRequest与当前请求关联的对象

    ${#request.getAttribute(‘foo‘)}
    ${#request.getParameter(‘foo‘)}
    ${#request.getContextPath()}
    ${#request.getRequestName()}

#session:直接访问javax.servlet.http.HttpSession与当前请求关联的对象

    ${#session.getAttribute(‘foo‘)}
    ${#session.id}
    ${#session.lastAccessedTime}

#servletContext:直接访问javax.servlet.ServletContext与当前请求关联的对象

    ${#servletContext.getAttribute(‘foo‘)}
    ${#servletContext.contextPath}

2.表达式实用程序对象

参考自Thymeleaf3.0官方文档第19条

2.1 执行信息

#execInfo:表达式对象,提供有关Thymeleaf标准表达式中正在处理的模板的有用信息

    ${#execInfo.templateName}
    ${#execInfo.templateMode}

    ${#execInfo.processedTemplateName}
    ${#execInfo.processedTemplateMode}

    ${#execInfo.templateNames}
    ${#execInfo.templateModes}

    ${#execInfo.templateStack}

2.2 消息内容

#messages:实用程序方法,用于获取变量表达式内的外部化消息,其方式与使用#{...}语法获得消息的方式相同

    ${#messages.msg(‘msgKey‘)}
    ${#messages.msg(‘msgKey‘, param1)}
    ${#messages.msg(‘msgKey‘, param1, param2)}
    ${#messages.msg(‘msgKey‘, param1, param2, param3)}
    ${#messages.msgWithParams(‘msgKey‘, new Object[] {param1, param2, param3, param4})}
    ${#messages.arrayMsg(messageKeyArray)}
    ${#messages.listMsg(messageKeyList)}
    ${#messages.setMsg(messageKeySet)}

    ${#messages.msgOrNull(‘msgKey‘)}
    ${#messages.msgOrNull(‘msgKey‘, param1)}
    ${#messages.msgOrNull(‘msgKey‘, param1, param2)}
    ${#messages.msgOrNull(‘msgKey‘, param1, param2, param3)}
    ${#messages.msgOrNullWithParams(‘msgKey‘, new Object[] {param1, param2, param3, param4})}
    ${#messages.arrayMsgOrNull(messageKeyArray)}
    ${#messages.listMsgOrNull(messageKeyList)}
    ${#messages.setMsgOrNull(messageKeySet)}

2.3 URI / URL

#uris:在Thymeleaf标准表达式内执行URI / URL操作的实用程序对象(尤其是URI/URL)

    // Escape/Unescape as a URI/URL path
    ${#uris.escapePath(uri)}
    ${#uris.escapePath(uri, encoding)}
    ${#uris.unescapePath(uri)}
    ${#uris.unescapePath(uri, encoding)}

    // Escape/Unescape as a URI/URL path segment (between ‘/‘ symbols)
    ${#uris.escapePathSegment(uri)}
    ${#uris.escapePathSegment(uri, encoding)}
    ${#uris.unescapePathSegment(uri)}
    ${#uris.unescapePathSegment(uri, encoding)}

    // Escape/Unescape as a Fragment Identifier (#frag)
    ${#uris.escapeFragmentId(uri)}
    ${#uris.escapeFragmentId(uri, encoding)}
    ${#uris.unescapeFragmentId(uri)}
    ${#uris.unescapeFragmentId(uri, encoding)}

    // Escape/Unescape as a Query Parameter (?var=value)
    ${#uris.escapeQueryParam(uri)}
    ${#uris.escapeQueryParam(uri, encoding)}
    ${#uris.unescapeQueryParam(uri)}
    ${#uris.unescapeQueryParam(uri, encoding)}

2.4 转换

#conversions:实用程序对象,允许在模板的任何位置执行转换服务

    ${#conversions.convert(object, ‘java.util.TimeZone‘)}
    ${#conversions.convert(object, targetClass)}

2.5 日期

#dates:java.util.Date对象的实用程序方法

    ${#dates.format(date)}
    ${#dates.arrayFormat(datesArray)}
    ${#dates.listFormat(datesList)}
    ${#dates.setFormat(datesSet)}

    ${#dates.formatISO(date)}
    ${#dates.arrayFormatISO(datesArray)}
    ${#dates.listFormatISO(datesList)}
    ${#dates.setFormatISO(datesSet)}

    ${#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‘)}

    ${#dates.day(date)}                    // also arrayDay(...), listDay(...), etc.
    ${#dates.month(date)}                  // also arrayMonth(...), listMonth(...), etc.
    ${#dates.monthName(date)}              // also arrayMonthName(...), listMonthName(...), etc.
    ${#dates.monthNameShort(date)}         // also arrayMonthNameShort(...), listMonthNameShort(...), etc.
    ${#dates.year(date)}                   // also arrayYear(...), listYear(...), etc.
    ${#dates.dayOfWeek(date)}              // also arrayDayOfWeek(...), listDayOfWeek(...), etc.
    ${#dates.dayOfWeekName(date)}          // also arrayDayOfWeekName(...), listDayOfWeekName(...), etc.
    ${#dates.dayOfWeekNameShort(date)}     // also arrayDayOfWeekNameShort(...), listDayOfWeekNameShort(...), etc.
    ${#dates.hour(date)}                   // also arrayHour(...), listHour(...), etc.
    ${#dates.minute(date)}                 // also arrayMinute(...), listMinute(...), etc.
    ${#dates.second(date)}                 // also arraySecond(...), listSecond(...), etc.
    ${#dates.millisecond(date)}            // also arrayMillisecond(...), listMillisecond(...), etc.

    ${#dates.create(year,month,day)}
    ${#dates.create(year,month,day,hour,minute)}
    ${#dates.create(year,month,day,hour,minute,second)}
    ${#dates.create(year,month,day,hour,minute,second,millisecond)}

    ${#dates.createNow()}
    ${#dates.createNowForTimeZone()}
    ${#dates.createToday()}
    ${#dates.createTodayForTimeZone()}

2.5 日历

#calendars:类似于#dates,但对于java.util.Calendar对象

    ${#calendars.format(cal)}
    ${#calendars.arrayFormat(calArray)}
    ${#calendars.listFormat(calList)}
    ${#calendars.setFormat(calSet)}

    ${#calendars.formatISO(cal)}
    ${#calendars.arrayFormatISO(calArray)}
    ${#calendars.listFormatISO(calList)}
    ${#calendars.setFormatISO(calSet)}

    ${#calendars.format(cal, ‘dd/MMM/yyyy HH:mm‘)}
    ${#calendars.arrayFormat(calArray, ‘dd/MMM/yyyy HH:mm‘)}
    ${#calendars.listFormat(calList, ‘dd/MMM/yyyy HH:mm‘)}
    ${#calendars.setFormat(calSet, ‘dd/MMM/yyyy HH:mm‘)}

    ${#calendars.day(date)}                // also arrayDay(...), listDay(...), etc.
    ${#calendars.month(date)}              // also arrayMonth(...), listMonth(...), etc.
    ${#calendars.monthName(date)}          // also arrayMonthName(...), listMonthName(...), etc.
    ${#calendars.monthNameShort(date)}     // also arrayMonthNameShort(...), listMonthNameShort(...), etc.
    ${#calendars.year(date)}               // also arrayYear(...), listYear(...), etc.
    ${#calendars.dayOfWeek(date)}          // also arrayDayOfWeek(...), listDayOfWeek(...), etc.
    ${#calendars.dayOfWeekName(date)}      // also arrayDayOfWeekName(...), listDayOfWeekName(...), etc.
    ${#calendars.dayOfWeekNameShort(date)} // also arrayDayOfWeekNameShort(...), listDayOfWeekNameShort(...), etc.
    ${#calendars.hour(date)}               // also arrayHour(...), listHour(...), etc.
    ${#calendars.minute(date)}             // also arrayMinute(...), listMinute(...), etc.
    ${#calendars.second(date)}             // also arraySecond(...), listSecond(...), etc.
    ${#calendars.millisecond(date)}        // also arrayMillisecond(...), listMillisecond(...), etc.

    ${#calendars.create(year,month,day)}
    ${#calendars.create(year,month,day,hour,minute)}
    ${#calendars.create(year,month,day,hour,minute,second)}
    ${#calendars.create(year,month,day,hour,minute,second,millisecond)}

    ${#calendars.createForTimeZone(year,month,day,timeZone)}
    ${#calendars.createForTimeZone(year,month,day,hour,minute,timeZone)}
    ${#calendars.createForTimeZone(year,month,day,hour,minute,second,timeZone)}
    ${#calendars.createForTimeZone(year,month,day,hour,minute,second,millisecond,timeZone)}

    ${#calendars.createNow()}
    ${#calendars.createNowForTimeZone()}
    ${#calendars.createToday()}
    ${#calendars.createTodayForTimeZone()}

2.5 数字

#numbers:用于数字对象的实用方法

    ${#numbers.formatInteger(num,3)}
    ${#numbers.arrayFormatInteger(numArray,3)}
    ${#numbers.listFormatInteger(numList,3)}
    ${#numbers.setFormatInteger(numSet,3)}

    ${#numbers.formatInteger(num,3,‘POINT‘)}
    ${#numbers.arrayFormatInteger(numArray,3,‘POINT‘)}
    ${#numbers.listFormatInteger(numList,3,‘POINT‘)}
    ${#numbers.setFormatInteger(numSet,3,‘POINT‘)}

    ${#numbers.formatDecimal(num,3,2)}
    ${#numbers.arrayFormatDecimal(numArray,3,2)}
    ${#numbers.listFormatDecimal(numList,3,2)}
    ${#numbers.setFormatDecimal(numSet,3,2)}

    ${#numbers.formatDecimal(num,3,2,‘COMMA‘)}
    ${#numbers.arrayFormatDecimal(numArray,3,2,‘COMMA‘)}
    ${#numbers.listFormatDecimal(numList,3,2,‘COMMA‘)}
    ${#numbers.setFormatDecimal(numSet,3,2,‘COMMA‘)}

    ${#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‘)}

    ${#numbers.formatCurrency(num)}
    ${#numbers.arrayFormatCurrency(numArray)}
    ${#numbers.listFormatCurrency(numList)}
    ${#numbers.setFormatCurrency(numSet)}

    ${#numbers.formatPercent(num)}
    ${#numbers.arrayFormatPercent(numArray)}
    ${#numbers.listFormatPercent(numList)}
    ${#numbers.setFormatPercent(numSet)}

    ${#numbers.formatPercent(num, 3, 2)}
    ${#numbers.arrayFormatPercent(numArray, 3, 2)}
    ${#numbers.listFormatPercent(numList, 3, 2)}
    ${#numbers.setFormatPercent(numSet, 3, 2)}

    ${#numbers.sequence(from,to)}
    ${#numbers.sequence(from,to,step)}

2.6 字符串

#strings:String对象的实用方法

    ${#strings.toString(obj)}                           // also array*, list* and set*

    ${#strings.isEmpty(name)}
    ${#strings.arrayIsEmpty(nameArr)}
    ${#strings.listIsEmpty(nameList)}
    ${#strings.setIsEmpty(nameSet)}

    ${#strings.defaultString(text,default)}
    ${#strings.arrayDefaultString(textArr,default)}
    ${#strings.listDefaultString(textList,default)}
    ${#strings.setDefaultString(textSet,default)}

    ${#strings.contains(name,‘ez‘)}                     // also array*, list* and set*
    ${#strings.containsIgnoreCase(name,‘ez‘)}           // also array*, list* and set*

    ${#strings.startsWith(name,‘Don‘)}                  // also array*, list* and set*
    ${#strings.endsWith(name,endingFragment)}           // also array*, list* and set*

    ${#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*

    ${#strings.prepend(str,prefix)}                     // also array*, list* and set*
    ${#strings.append(str,suffix)}                      // also array*, list* and set*

    ${#strings.toUpperCase(name)}                       // also array*, list* and set*
    ${#strings.toLowerCase(name)}                       // also array*, list* and set*

    ${#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>

    ${#strings.trim(str)}                               // also array*, list* and set*

    ${#strings.length(str)}                             // also array*, list* and set*

    ${#strings.abbreviate(str,10)}                      // also array*, list* and set*

    ${#strings.capitalize(str)}                         // also array*, list* and set*
    ${#strings.unCapitalize(str)}                       // also array*, list* and set*

    ${#strings.capitalizeWords(str)}                    // also array*, list* and set*
    ${#strings.capitalizeWords(str,delimiters)}         // also array*, list* and set*

    ${#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*

    ${#strings.equals(first, second)}
    ${#strings.equalsIgnoreCase(first, second)}
    ${#strings.concat(values...)}
    ${#strings.concatReplaceNulls(nullValue, values...)}

    ${#strings.randomAlphanumeric(count)}

2.7 对象

#objects:一般对象的实用方法

    ${#objects.nullSafe(obj,default)}
    ${#objects.arrayNullSafe(objArray,default)}
    ${#objects.listNullSafe(objList,default)}
    ${#objects.setNullSafe(objSet,default)}

2.8 布尔值

#bools:用于布尔值评估的实用方法

    ${#bools.isTrue(obj)}
    ${#bools.arrayIsTrue(objArray)}
    ${#bools.listIsTrue(objList)}
    ${#bools.setIsTrue(objSet)}

    ${#bools.isFalse(cond)}
    ${#bools.arrayIsFalse(condArray)}
    ${#bools.listIsFalse(condList)}
    ${#bools.setIsFalse(condSet)}

    ${#bools.arrayAnd(condArray)}
    ${#bools.listAnd(condList)}
    ${#bools.setAnd(condSet)}

    ${#bools.arrayOr(condArray)}
    ${#bools.listOr(condList)}
    ${#bools.setOr(condSet)}

2.9 Arrays数组

#arrays:数组的实用方法

    ${#arrays.toArray(object)}
    ${#arrays.toStringArray(object)}
    ${#arrays.toIntegerArray(object)}
    ${#arrays.toLongArray(object)}
    ${#arrays.toDoubleArray(object)}
    ${#arrays.toFloatArray(object)}
    ${#arrays.toBooleanArray(object)}
    ${#arrays.length(array)}
    ${#arrays.isEmpty(array)}
    ${#arrays.contains(array, element)}
    ${#arrays.containsAll(array, elements)}

2.10 List集合

#lists:list集合的实用方法

    ${#lists.toList(object)}
    ${#lists.size(list)}
    ${#lists.isEmpty(list)}
    ${#lists.contains(list, element)}
    ${#lists.containsAll(list, elements)}
    ${#lists.sort(list)}
    ${#lists.sort(list, comparator)}

2.11 Set集合

#sets:set集合的实用方法

${#sets.toSet(object)}

${#sets.size(set)}
${#sets.isEmpty(set)}

${#sets.contains(set, element)}
${#sets.containsAll(set, elements)}

2.12 Map集合

#maps:map集合的实用方法

${#maps.size(map)}

${#maps.isEmpty(map)}

${#maps.containsKey(map, key)}
${#maps.containsAllKeys(map, keys)}
${#maps.containsValue(map, value)}
${#maps.containsAllValues(map, value)}

2.13 聚集体

#aggregates:在数组或集合上创建聚合的实用程序方法

${#aggregates.sum(array)}
${#aggregates.sum(collection)}

${#aggregates.avg(array)}
${#aggregates.avg(collection)}

2.14 编号

#ids:用于处理可能重复的id属性的实用程序方法(例如,作为迭代的结果)

${#ids.seq(‘someId‘)}

${#ids.next(‘someId‘)}
${#ids.prev(‘someId‘)}

以上是关于Thymeleaf内置对象表达式大全的主要内容,如果未能解决你的问题,请参考以下文章

从零开始的Spring Boot(6Thymeleaf内置对象及表达式大全)

第十一节:Thymeleaf内置对象

Thymeleaf 速查文档

thymeleaf学习

Java基础总结之Thymeleaf详解

使用片段时 Intellij 无法正确识别 Thymeleaf 模型变量