Results 1 to 4 of 4

Thread: Message Question

  1. #1
    Join Date
    Aug 2004
    Posts
    5

    Default Message Question

    I'm copying this over from the list and wanted to continue the discussion.

    You should be able to make the argument itself MessageSourceResolvable. So…



    MessageSourceResolvable nameResolvable = new MessageSourceResolvable() {

    public String[] getCodes() {

    return new String[] { “name” };

    }

    public Object[] getArgs() {

    return null;

    }

    public String getDefaultMessage() {

    return “Name”;

    }

    };



    Then:



    Object[] objArr = new Object[]{nameResolvable};


    errors.rejectValue("name", "error.requried", objArr, "Name is required.");


    We use this approach in the declarative validation stuff in the sandbox when constructing default error messages to display from the rules associated with domain object properties.



    Keith




    --------------------------------------------------------------------------------

    From: springframework-user-admin@lists.sourceforge.net [mailto:springframework-user-admin@lists.sourceforge.net] On Behalf Of Brandon Goodin
    Sent: Monday, August 23, 2004 12:52 AM
    To: springframework-user@lists.sourceforge.net
    Subject: [Springframework-user] Message Question



    Greetings all,

    I set up my messages resource in my xyz-servlet.xml as such:

    <bean id="messageSource" class="org.springframework.context.support.Resourc eBundleMessageSource" >

    <property name="basename"><value>xyz-messages</value></property>

    </bean>

    I have the following message in my xyz-messages.properties:

    error.required={0} is required.

    I also have the name of my field in the xyz-messages.properties file as:

    xyz.name=Name

    In my Validator class, when a validation fails on the field I want to place the field name into the Object array, that is passed into the rejectValue method, using the message (xyz.name) from the messages.properties. How would I accomplish this?

    String nameField = <how do I get the message?>;

    Object[] objArr = new Object[]{nameField};

    errors.rejectValue("name", "error.requried", objArr, "Name is required.");

    Thanks,
    Brandon
    Brandon Goodin

  2. #2
    Join Date
    Aug 2004
    Posts
    5

    Default

    So, for every field lablel that i have i will have to implement this interface? This seems like a ridiculous amount of work to simply get the form element label (contained in a message.properties) resource into an error message arguments Object[]. Are you sure this is the only way?
    Brandon Goodin

  3. #3
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    You could of course create an abstract superclass e.g FieldLabel:

    Code:
    private FieldLabel implements MessageSourceResolvable &#123;
    
    private String fieldName;
    
    public FieldLabel&#40;String fieldName&#41; &#123;
        this.fieldName = fieldName;
    &#125;
    
    public String&#91;&#93; getCodes&#40;&#41; &#123; 
        return new String&#91;&#93; &#123; fieldName &#125;; 
    &#125; 
    
    public Object&#91;&#93; getArgs&#40;&#41; &#123; 
        return null; 
    &#125; 
    
    public String getDefaultMessage&#40;&#41; &#123; 
        return fieldName; 
    &#125; 
    
    &#125;
    then just say:

    Code:
    Object&#91;&#93; args = new Object&#91;&#93; &#123; new FieldLabel&#40;"name"&#41; &#125;;
    ...

  4. #4
    Join Date
    Aug 2005
    Posts
    2

    Default

    is this still the only way to retrieve a label from a resource bundle?

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. Replies: 3
    Last Post: May 16th, 2005, 11:14 PM
  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
  •