Overview of This Lesson

Refresh this page because I am probably still making changes to it.

You can use special Thymeleaf objects to perform extra tasks like processing strings, formatting numbers, and working with lists.

Helpful Resources

Thymeleaf Objects

Thymeleaf comes with several utility objects that you can use for common tasks such as working with dates and times, formatting numbers, and string operations. There are several objects, but I've only included the ones you might find useful. Feel free to explore the others in the Thymeleaf documentation and tutorials.

Exercises

  1. Write the code for a paragraph element that displays the year of a date stored in the model attribute that has the key "today".
  2. Write the code for any block element that only renders if a string stored in the model attribute "itemId" contains the value "xx". The elment should display the value in the itemId model attribute in all upper-case letters.
  3. Write the code that displays a table only if an array stored in the model attribute "bookList" contains elements.
  4. Write the code that displays an unordered list of Inventory objects with a list item for each inventory object in a list stored as the model attribute "inventory". The inventory items should be displayed in sorted order (assume the Inventory bean implements Comparable)
  1. <p th:text="${#dates.year(today)}">year</p>
  2. <div th:text="${#strings.toUpperCase(itemId)}" th:if="${#strings.contains(itemId, 'xx')}"></p> (will also work with #strings.indexOf(..) gt 0)
  3. <table th:if="${not #arrays.isEmpty(bookList)}"> .... </table>
  4. <ul>
      <li th:each="inv : ${#lists.sort(inventory)}">item</li>
    </ul>