Results 1 to 4 of 4

Thread: Sweet! Want to learn more - an i18n message question

  1. #1

    Default 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. #2
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

    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.

    hth,
    Darren Davison.
    Public Key: 0xE855B3EA

  3. #3
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    667

    Default 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:

    Code:
    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,

    Andrew

  4. #4

    Default

    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

Similar Threads

  1. Replies: 7
    Last Post: Sep 29th, 2005, 05:51 AM
  2. UpgradeAcegi Security System from 0.6.1 to 0.8.3
    By mannobug in forum Security
    Replies: 3
    Last Post: Sep 23rd, 2005, 07:00 PM
  3. Message Question
    By phasews in forum Web
    Replies: 3
    Last Post: Sep 14th, 2005, 07:54 AM
  4. Channel and message transformation question
    By Alarmnummer in forum Architecture
    Replies: 12
    Last Post: May 11th, 2005, 05:06 PM
  5. Replies: 3
    Last Post: Apr 29th, 2005, 06:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •