0

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?

2 Answers 2

1

You can try Errors.rejectValue(String field, String errorCode, String defaultMessage) method. I too had the same problem. In my case I used ...errors.rejectValue("firstName", "error.firstName", "plz enter ur first name!!.");

This should ideally use the defaultMessage in the scenario where it can't find the error code from message source.

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of using a ResourceBundleMessageSource use a custom MessageSource implementation, which back in a DB. Unfortunately Spring itself doesn't have such an implementation, as described in the jira issue: https://jira.springsource.org/browse/SPR-364

But implementing the MessageSource Interface is straightforward, you can even see a sample implementation attached to the jira issue: https://jira.springsource.org/secure/attachment/10261/JdbcMessageSource.java

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.