Sometimes we want to show messages to the user when he saves, deletes or updates an entity. To achieve this we can use messages that change depending on the action of the user (it’s dynamic) in the page view.
*We will suppose that you configured the messageSource bean in spring to get the messages from a property file.*
So for example, if we want to show “saved correctly” for an entity that was saved we have to use dynamic view messages. We can do this in 2 different ways:
1. You can create an attribute in the view as boolean and use that.
2. You can autowire the messageSource that you are using to get the properties from the file in the controller method.
Now the view will look like this:
Both are options are good solutions it all depends what you want to do and what you prefer. Feel free to leave your comments about this too.
In my opinion if a view has a lot of different dynamic messages it is better to use the second approach (getting the message from java) otherwise you will get something like this: