Thymeleaf utility methods for Arrays

January 04, 2020 No comments Thymeleaf Utility Arrays

1. Introduction

Thymeleaf is designed for the Java applications template engine. The engine has implemented several utility objects that can be used to perform special operations on template variables. In this article, we will present utility methods for Arrays. The implementation of Arrays utility methods can be found in the official Thymeleaf GitHub Repository.

2. Available utility methods for Arrays

Thymeleaf comes with several utility methods for Arrays.

Method Purpose Description
${#arrays.toArray(object)}

Converts to array, trying to infer array component class 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.toStringArray(object)}
${#arrays.toIntegerArray(object)}
${#arrays.toLongArray(object)}
${#arrays.toDoubleArray(object)}
${#arrays.toFloatArray(object)}
${#arrays.toBooleanArray(object)}

Convert to arrays of the specified component class
${#arrays.length(array)}

Get array length
${#arrays.isEmpty(array)}

Check whether array is empty
${#arrays.contains(array, element)}
${#arrays.containsAll(array, elements)}

Check if array contains given element or all elements

3. Conclusion

In this article, we presented Thymeleaf utility methods for Arrays which are commonly used objects in applications. The utility should be useful to check the size of an array, check if an array contains a specific element or group of elements.

{{ message }}

{{ 'Comments are closed.' | trans }}