7

we are using Spring MessageSource to build error messages in our app.

We populate our error messages like this

dobInvalid = The DOB supplied {0} is invalid

We want to use named parameters so we can do

dobInvalid = The DOB supplied {dob} is invalid

Looking in the Api docs for getMessage it appears to suggest you can do this http://static.springsource.org/spring/docs/1.2.x/api/org/springframework/context/MessageSource.html

args - Array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message), or null if none.

Obviously we can write our own but was wondering if spring can do it and if anyone can provide an example or using named parameters rather positional parameters.

Cheers Mark

Mark Hanlon
  • 121
  • 1
  • 3

1 Answers1

6

AIUI, Spring MessageSource works with JDK MessageFormat, so there is no such a named parameter. {1,date} is an example, where "date" refers to formatType, no to an arbitrary named parameter.

The general form of a parameter is:

{ ArgumentIndex , FormatType , FormatStyle }
Camilo Silva
  • 8,283
  • 4
  • 41
  • 61