Grails not using message.properties for Errors messages?
I'm trying to get at the localized form of a simple error code, but I can't seem to get it to parameterize as it should. In my integration test, I have this, just to illustrate the issue:
for( FieldError error : errors.fieldErrors )
{
println error.toString()
}
In some manual binding, I have this:
myObject.errors.rejectValue("field1.field2", "typeMismatch.java.lang开发者_运维问答.Integer", [value] as Object[], "")
But when I print I get this:
Field error in object 'com.me.MyObject' on field 'field1.field2': rejected value [1];
codes
[typeMismatch.java.lang.Integer.com.me.MyObject.field1.field2,typeMismatch.java.lang.Integer.field1.field2,typeMismatch.java.lang.Integer.field2,typeMismatch.java.lang.Integer.int,typeMismatch.java.lang.Integer]
Why doesn't the string from message.properties
get used?
typeMismatch.java.lang.Integer=Property {0} must be a valid number
You can use the <g:message>
tag as ataylor has suggested or the MessageSource Spring bean defined by Grails. An example of using this in a controller is shown below
import org.springframework.context.MessageSource
import org.springframework.web.servlet.support.RequestContextUtils as RCU
class SomeController {
MessageSource messageSource
def someAction = {SomeObject someObject ->
for (error in someObject.errors) {
// lookup the error message
Locale locale = RCU.getLocale(request)
String errorMsg = messageSource.getMessage(error)
println errorMsg
}
}
}
Pass your error through the g:message
tag to get the message from message.properties
. In a controller, you can write something like this:
for( FieldError error : errors.fieldErrors )
{
println g.message(error: error)
}
Alternatively, you can use the messageSource
spring bean:
def messageSource
for( FieldError error : errors.fieldErrors )
{
def locale = RequestContextUtils.getLocale(request)
println messageSource.getMessage(error, locale)
}
精彩评论