2013-03-14 85 views
2

Grails 2.2.0在域对象约束中使用grails配置值

如何访问Grails域对象约束中的自定义配置变量。

我想有这样的事情:

class User { 

     def grailsApplication 

     String name 

     static constraints = { 
      name size: grailsApplication.config.maxlength 
     } 

    } 

但它失败,“没有这样的属性:grailsApplication”。我试图通过遵循getting grails 2.0.0M1 config info in domain object, and static scope?中的建议来使其工作,但尚未设法使任何组合工作。

如何访问域对象约束中的配置?另外,如何在域约束的单元测试中处理这种情况?

+0

你尝试'这.grailsApplication ...'? – 2013-03-14 20:40:44

+2

Holders.getGrailsApplication()。config.maxlength – 2013-03-14 21:21:30

+2

@SérgioMichels或更短:Holders.config.maxlength – codelark 2013-03-19 15:19:14

回答

4

可以使用grails.util.Holders类来获得访问配置对象,如下所示:

Config.groovy

myMaxSize = 10 

在你的领域类:

class User { 
    String name 

    static constraints = { 
     name minSize: Holders.config.myMaxSize 
    } 
}