This tutorial is about how to highlight the currently active page in your navigation with Thymeleaf and Spring Boot. Highlighting the active page is a known usability pattern and should help users to find their way around your web application. Consider this example: In the navigation above, the navigation item “Cronjob Monitoring” is highlighted. The menu is generated with this Thymeleaf code: <nav class="mdl-navigation"> <a th:classappend="${#request.requestURI.startsWith(navItem.getLink()) ? 'mdl-navigation__link-active':''}" th:each="navItem: ${navigation}" th:id="${navItem.getIdentifier()}" class="mdl-navigation__link" th:href="@{${navItem.getLink()}}" th:text="${navItem.getName()}"></a> </nav> By using th:each, a link ...

Question: In my Thymeleaf template, I want to output the size of a list of objects. For example, for the list List<String> list = Arrays.asList("Go", "Python", "Scala"); the result should be 3. How is this possible using just Thymeleaf syntax? You can use the following expression: <span th:text="${#lists.size(myList)}">[Number of Elements]</span> And just in case you want to check if the list is empty, there is also an easy method for that: <span th:if="${#lists.isEmpty(myList)}">This list is empty</span> ...

Bernhard Knasmüller on Software Development