Utility objects

Thymeleaf provides some utility objects, that can be used in expressions. Most of them

  • #strings - methods for String objects
  • #numbers - methods for formatting numeric objects
  • #bools - methods for boolean
  • #maps - methods for maps
  • #arrays - methods for arrays
  • #lists - methods for lists
  • #sets - methods for sets
  • #aggregates - methods for aggregating arrays and collections
  • #dates - methods for Date objects
  • #calendars - methods for Calendar objects
  • #objects - methods for objects in general
  • #uris - methods for performing URI/URL operations
  • #messages - methods for obtaining externalized messages inside variables expressions
  • #execInfo - methods for providing a information about the template being processed
<p th:text="${#strings.isEmpty(string)}"></p>
<p th:text="${#strings.defaultString(string,'Default string')}"> </p>

strings

method description
toString(obj) Null-safe conversion object to string.
trim(str) Trims a string. Same with arrays, lists or sets.
trim(str) Computes length of string. Same with arrays, lists or sets.
isEmpty(str)
arrayIsEmpty(array)
listIsEmpty(lst)
setIsEmpty(set)
Checks whether a <str> is empty (or null). Performs a trim() operation before check. Same with arrays, lists or sets.
defaultString(text ,default)
arrayDefaultString(arr, default)
listDefaultString(lst, default)
setDefaultString(set, default)
Performs an 'isEmpty()' check on a string and return it if false, defaulting to another specified string if true. Same with arrays, lists or sets.
contains(name,'ez')
containsIgnoreCase(name,'ez')
Check whether a fragment is contained in a String. Same with arrays, lists or sets.
startsWith(name, frag)
endsWith(name, frag)
Check whether a String starts or ends with a fragment. Same with arrays, lists or sets.
indexOf(str, frag)
substring(str,
    from,
    to)
substringAfter(str, prefix)
substringBefore(str, suffix)
replace(name, frag, to)
Substring-related operations. Same with arrays, lists or sets.
prepend(str, prefix)
append(str, suffix)
Appends and prepends a string fragment to string. Same with arrays, lists or sets.
toUpperCase(str)
toLowerCase(str)
Change case. Same with arrays, lists or sets.
capitalize(str)
unCapitalize(str)
Changes case of the first character to upper-case or lower-case. Same with arrays, lists or sets.
capitalizeWords(str)
capitalizeWords(str,
    delimiters)
Convert the first character of every word to upper-case. Same with arrays, lists or sets.
escapeXml(str)
escapeJava(str)
escapeJavaScript(str)
unescapeJava(str)
unescapeJavaScript(str)
Escaping and unescaping of strings.
equals(first, second)
equalsIgnoreCase(first,
    second)
Null-safe case sensitive/insensitive comparison of strings.
concat(values...)
concatReplaceNulls(
    nullValue,
    values...)
Null-safe concatenation of strings.