Welcome, User! in Thymeleaf and Spring

A little ditty for you on how to create a “Welcome, User!” string in Thymeleaf and Spring, or in other terminology, a parameterized message property. We’re referring to the “Welcome, user!” shown below, from my NixMash Spring app on GitHub.┬áNot very creative with a User name of “user,” but it’s the thought that counts.

Here’s the messages.properties string.

navbar.welcome=Welcome, {0}!

And the Thymeleaf HTML.

<p th:utext="#{navbar.welcome(${currentUser.username})}">[welcome]</p>

Remember, we’re passing the CurrentUser object in a ControllerAdvice method for all pages.

I told you it was a little ditty…