Warning: A non-numeric value encountered in /home/ricston2/public_html/blogarchive/wp-content/themes/Divi/functions.php on line 5766

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.

For example:

 

And then use spEL (Spring Expression Language) to know what message to use depending on the boolean attribute used (you can also use javascript if you want)

 

2. You can autowire the messageSource that you are using to get the properties from the file in the controller method.

Like so:

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: