Many applications are automatically tested on each commit inside a GitLab pipeline. If your application relies on a database such as PostgreSQL, it can be tempting to use an in-memory database such as H2 for tests because it is easier to set up and destroy for each execution of your test suite. While this works for simple applications, it is no longer possible once you rely on vendor-specific SQL features (e.g., usage of column types not available in H2). In ...

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 ...

Different deployment environments (e.g., development, testing, staging and production) often require that different pieces of code are switched on and off. For example, you only want to send real emails in production and write to an email log in all other environments instead. Or your code to initialise the database should only run in the staging environment. A simple but hard to maintain way for such conditional code executions is to define a globally available variable such as isProductionSystem and write ...

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> ...

Creating and persisting business objects using Spring Boot is amazingly easy. Assume you create an API for simple CRUD methods and want to create an entity based on data entered by your frontend users. In the old times, you would probably populate several POST fields in a key-value style and create your business object manually from those fields. Spring Boot offers an easier solution. As long as your internal data model equals the frontend’s data model, you can use the ...

In this article, I’ll show how to set up a simple development environment for working with Spring Boot inside a VirtualBox Ubuntu Server. You will learn how to configure port forwarding for interacting with the development server set up shared folders for using an IDE on your client to edit code on the server build and execute the application using maven Let’s get started! Prerequisites You need: VirtualBox installed on your host (i.e., your desktop or laptop) A virtual machine ...

Bernhard Knasmüller on Software Development