I have a Spring mvc application with a Controller, Model, Service, and Validation classes. Everything works fine. In developing what I currently have, I stored the validation messages in a messages.properties file -which I registered in a configuration class like this:
@Bean
public MessageSource messageSource() {
final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("[package name]/messages/messages");
return messageSource;
}
In the Validator, I add a validation message if the user didn't answer a field such as this:
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "age", "required", new Object[] {"Age"});
In the messages.properties file I have this (above 'age' is the name of the model object, 'required' is the key used to retrieve the message in the messages.properties file):
required = {0} is required.
Ok, again -this all works fine. But what I'd like to do is externalize the messages to a database (basically, these are corporate messages that all applications use).
My question is -how do I get Spring to use the database instead of messages.properties? Or is I do use messages.properties, to only store the message's id (database key) instead of the message itself?
I have a couple ideas of what to do -but no idea how to do either one. I 'think' that the Spring-form tag library ends up performing the work of getting the actual message. Perhaps I could somehow intercept that call? Or maybe somehow change the messageSource to something else rather than defining the messages.properties file.
Anyways, does anyone have any ideas?