Thymeleaf utility methods for Objects

January 04, 2020 No comments Thymeleaf Utility Objects

1. Introduction

Thymeleaf is a Java template for application written in Java. In this article, we will present utility methods for Objects in general. The implementation of Object utility methods can be found in the official Thymeleaf GitHub Repository.

2. Available utility methods for Object

Thymeleaf provides several methods to check if the given object is null.

Method Purpose Description
${#objects.nullSafe(obj,default)}
${#objects.arrayNullSafe(objArray,default)}
${#objects.listNullSafe(objList,default)}
${#objects.setNullSafe(objSet,default)}
If given obj is null then default value will be returned otherwise (object is not null) obj will be returned Also works with arrays, lists or sets

3. Conclusion

In this article, we presented Thymeleaf utility methods for Objects in general. Any given object can be check with the provided method and if it is null the default String will be presented. This inline method should save a lot of work.

{{ message }}

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