自定义错误消息不适用于带有grails的gorm

自定义错误消息不适用于带有grails的gorm,grails,gorm,Grails,Gorm,似乎无法在Grails2.3.7上获得自定义验证消息 class Team { int id String email static mapping = { id column:'TEAM_ID', generator:'native' email column:'TEAM_EMAIL' } static constraints = { email unique:true } messages.properti

似乎无法在Grails2.3.7上获得自定义验证消息

class Team {
   int id
   String email

   static mapping = {
     id     column:'TEAM_ID', generator:'native'
     email  column:'TEAM_EMAIL' 
   }
   static constraints = { 
     email  unique:true
   }
messages.properties(尝试了所有这些…)

“我的服务”会发现验证问题,但从未使用自定义错误消息:

Team t = new Team(...)
if(!t.validate())
   println t.errors
输出

Field error in object 'Team' on field 'email': rejected value.... ...
default message [Property [{0}] of class [{1}] with value [{2}] must be unique]
不确定问题是什么,根据公约:

[类名].[属性名].[约束代码]

额外配置 构建配置 数据来源
根据位于的唯一约束文档页面,它是
className.propertyName.unique
,但是
println t.errors
将始终像您看到的那样-结果不是很有用。要查看已解析/已翻译的消息,您可以在测试或测试应用程序中测试您正在执行的操作,并且您可以使用它来显示已解析的消息:

// dependency injection in controller/service/etc.
def messageSource

...

Locale locale = Locale.getDefault()
def stringsByField = [:].withDefault { [] }

for (fieldErrors in t.errors) {
   for (error in fieldErrors.allErrors) {
      String message = messageSource.getMessage(error, locale)
      stringsByField[error.field] << message
   }
}

println stringsByField
//控制器/服务/etc中的依赖项注入。
def消息源
...
Locale=Locale.getDefault()
def stringsByField=[:]。默认值为{[]}
for(t.errors中的字段错误){
for(fieldErrors.allErrors中的错误){
String message=messageSource.getMessage(错误,区域设置)

stringsByField[error.field]什么是messageSource?这是一个预定义的bean吗?是的,所以您需要依赖项将其作为类范围变量注入工件(控制器、服务等)中。它可以工作,+1表示
MapWithDefault
,但
messageSource.getMessage(错误、区域设置)
响应需要2-3秒,即使是对相同错误条件的重复调用也需要2-3秒。我使用的是一个简单的测试应用程序,资源最少,只有一个域类和服务;我已经用额外的数据更新了我的问题;关于为什么调用需要如此长的时间,有什么想法吗?不知道,这应该很快。这是Grails用来解决问题的同一个bean在GSP中显示消息-您是否看到类似的慢度?请注意,它在生产中会更快,因为它会主动缓存,而在开发模式下支持重新加载(尽管它仅在检测到或手动触发更改时重新加载源文件)。
dependencies {
    compile('com.oracle:ojdbc6:11.2.0')
}
plugins {
    build ":tomcat:7.0.52.1"
    runtime ":hibernate:3.6.10.9"
}
 dialect  =  "org.hibernate.dialect.Oracle10gDialect"
 hibernate {
    cache.use_second_level_cache = false
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
    singleSession = false 
}
// dependency injection in controller/service/etc.
def messageSource

...

Locale locale = Locale.getDefault()
def stringsByField = [:].withDefault { [] }

for (fieldErrors in t.errors) {
   for (error in fieldErrors.allErrors) {
      String message = messageSource.getMessage(error, locale)
      stringsByField[error.field] << message
   }
}

println stringsByField