Named parameters, not positional for Spring MessageSource.getMessage ()

we use Spring MessageSource to create error messages in our application.

We fill out our error messages as follows

dobInvalid = The DOB supplied {0} is invalid 

We want to use named parameters so that we can do

 dobInvalid = The DOB supplied {dob} is invalid 

Looking at the Api docs for getMessage, it looks like you can do this http://static.springsource.org/spring/docs/1.2.x/api/org/springframework/context/MessageSource.html

args - an array of arguments to be filled for the parameters within the message (the parameters look like "{0}", "{1, date}", "{2, time}" within the message), or null if not.

Obviously, we can write our own, but it was interesting if Spring could do this, and if anyone could provide an example or use named parameters rather than positional parameters.

Cheers Mark

+4
source share
1 answer

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

General view of the parameter:

 { ArgumentIndex , FormatType , FormatStyle } 
+2
source

Source: https://habr.com/ru/post/1436170/


All Articles