PDA

View Full Version : Message Question



phasews
Aug 23rd, 2004, 01:19 AM
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.ResourceBundle MessageSource" >

<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

phasews
Aug 23rd, 2004, 01:22 AM
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?

Keith Donald
Aug 23rd, 2004, 07:34 AM
You could of course create an abstract superclass e.g FieldLabel:



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:



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

mrmikeflynn
Sep 14th, 2005, 07:54 AM
is this still the only way to retrieve a label from a resource bundle?