Wednesday, October 9, 2019

Different Expression Utility Objects in Thymeleaf With Examples

I bet every one of you must have performed one of the below tasks while development irrespective of the programming language you use for development:
  • Created a Utility class or 
  • Added utility methods in the existing Utility class or 
  • Used already written, existing Utility class methods for performing any common job 
You all would agree that knowing the available Utility classes in any language makes the developer's life easy. Therefore today we would be looking at various utility objects provided by Thymeleaf that help developers in performing common tasks in Thymeleaf expression like performing date formatting, performing numeric operations, performing String-related operation, etc. These utility objects are also known as Expression Utility Objects.

Expression Utility Objects


Below is the list of utility objects:
  • #dates: utility methods for java.util.Date objects: formatting, component extraction, etc.
    ## See javadoc API for class org.thymeleaf.expression.Dates
    <span th:if="${assignment.availableOn > #dates.createNow()}">
    <span th:text="${#dates.format(patient.currentDate, 'dd-MMM-yyyy')}">10-09-2019</span>
    
  • #calendars: analogous to #dates, but for java.util.Calendar objects.
  • #numbers: utility methods for formatting numeric objects.
    ## See javadoc API for class org.thymeleaf.expression.Numbers
    ## format decimal number by setting minimum integer digits and exact decimal digits
    <p th:with="num=${1.23345}" th:text="${#numbers.formatDecimal(num,2,3)}">01.233</p>
    <div th:text="${#numbers.formatDecimal(totalScore*0.1*100/totalMaxScore,0,0)}+'%'">
     80%</div>
    
  • #strings: utility methods for String objects: contains, startsWith, prepending/appending, etc.
    Example:
    ## See javadoc API for class org.thymeleaf.expression.Strings
    <span th:with="firstName=${#strings.substring(userModel.username,0,#strings.indexOf(userModel.username,' '))},
            lastName=${#strings.substring(userModel.username,#strings.indexOf(userModel.username,' ') + 1,#strings.length(userModel.username))},
            username=${ lastName + ', ' + firstName}" 
          th:data-username="${#strings.contains(userModel.username,' ') ? username : userModel.username}" 
          th:text="${username}"></span> 
    
  • #objects: utility methods for objects in general.
  • #bools: utility methods for boolean evaluation.
  • #arrays: utility methods for arrays.
  • #lists: utility methods for lists.
    ## See javadoc API for class org.thymeleaf.expression.Lists
    ## topicToFirstReadingTypeSubTopicMap is model attribute
    <select th:name="assetTopics" th:multiple="multiple">
     <option th:each="row : ${topicToFirstReadingTypeSubTopicMap}" 
             th:text="${row.key.label}" th:value="${row.value.nodeId}"  
             th:selected="${#lists.contains(objIdListForAsset, row.value.nodeId)}">Topic
     </option>
    </select> 
    ## assets is model attribute
    <div th:if="${not #lists.isEmpty(assets)}">...</div>
    
  • #sets: utility methods for sets.
  • #maps: utility methods for maps.
  • #aggregates: utility methods for creating aggregates on arrays or collections.
    ## See javadoc API for class org.thymeleaf.expression.Aggregates
    ## initially created a list of elements statically for demo and then used utilities
    <p th:with="array=${ {1,2,3} }" th:text="${#aggregates.sum(array)}">6</p>
    <p th:with="array=${ {1,2,3} }" th:text="${#aggregates.avg(array)}">2</p>
    
  • #messages: utility methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax.
  • #ids: utility methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).
  • #execInfo: information about the template being processed.
  • #uris: methods for escaping parts of URLs/URIs.
  • #conversions: methods for executing the configured conversion service (if any).
Hoping that, these expression utility objects and given examples would help you while development. Happy Learning!! ☺

No comments:

Post a Comment