Announcement Announcement Module
No announcement yet.
Sweet! Want to learn more - an i18n message question Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sweet! Want to learn more - an i18n message question

    I pulled error messages out from my validator classes into the message property files yesterday. With previous experience in i18n J2EE application development, my this i18n experience is quite sweet: keeping the message keys in the class and text in the message file. I am wondering whether there is the similar approach for any messages I want to show in the success view of a controller class, including SimpleFormController. After browsing over the country sample application and the document, I am unable to find any related information.

    Thanks for your input.

  • #2
    if your application context contains the messageSource bean referencing your resource bundles, then yes. In JSP use <spring:message> tag, in Velocity use #springMessage() macro and in FreeMarker use <@spring.message> macro.



    • #3
      Getting messages from within controllers

      I know your question was some time ago, but you didn't indicate whether the first reply answered it, so here's my $0.02...

      Anyway, the first responder told you how to get a message from within a view, but you asked about getting a message from within a controller. This is easy from within a SimpleFormController or indeed any ApplicationContextAware class:

      Object&#91;&#93; messageArgs = &#123;"My arg one","My arg two"&#125;; // if you need them, otherwise pass null
      String message = getApplicationContext&#40;&#41;.getMessage&#40;"my.message.code", messageArgs, "My Default Message", request.getLocale&#40;&#41;&#41;;
      Hope this helps,



      • #4
        Thanks Andrew and Darren for your responses.

        My question is whether or not that message keys and message parameters can be set in the controller classes and message bodies are in the message property files as how the error message can be generated in some methods of the ValidationUtils and Errors classes.

        For example, a validator class:

        public class EntryValidator implements Validator {

        private final static int NORMAL_FIELD_LENGTH = 80;

        // ...

        public void validate(Object obj, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "entry.subject", "SUBJECT_REQUIRED", "Subject is required.");

        EntryForm form = (EntryForm)obj;
        if(form.getEntry().getSubject().length() > NORMAL_FIELD_LENGTH)
        errors.rejectValue("entry.subject", "TITLE_TOO_LONG", new Object[] {new Integer(NORMAL_FIELD_LENGTH)},
        "The length of subject shall not exccess " + NORMAL_FIELD_LENGTH);


        And the message property file:
        SUBJECT_REQUIRED=Please enter your subject
        TITLE_TOO_LONG=The subject line can't have more than {0} characters