在Grails taglib中获取动态Config参数的问题

时间:2010-04-16 20:04:38

标签: grails config

我想要一个动态配置参数:

    String srcProperty = "${attrs ['src']}.audio" + ((attrs['locale'])? "_${attrs['locale']}" : '')

    assert srcProperty == "prompt.welcomeMessageOverrideGreeting.audio"

我的配置有:

prompt{
    welcomeMessageOverrideGreeting {
        audio = "/en/someFileName.wav"
        txt = "Text alternative for /en/someFileName.wav"
        audio_es = "/es/promptFileName.wav"
        txt_es = "Texto alternativo para /es/someFileName.wav"
    }
}

虽然这很好用:

String audio = "${config.prompt.welcomeMessageOverrideGreeting.audio}"

和:         断言“$ {config.prompt.welcomeMessageOverrideGreeting.audio}”==“/ en / someFileName.wav”

我无法让这个工作:

String audio = config.getProperty("prompt.welcomeMessageOverrideGreeting.audio")

3 个答案:

答案 0 :(得分:2)

它们不像那样存储,它们是分层存储的。 “config.prompt.welcomeMessageOverrideGreeting.audio”是从配置中获取“提示”的简写,然后是“welcomeMessageOverrideGreeting”,然后是“audio”。如果你想使用点符号,只需展平配置:

String audio = config.flatten().getProperty("prompt.welcomeMessageOverrideGreeting.audio")

答案 1 :(得分:2)

解决:这真的很难,但这是我发现的有助于获得动态属性:

String audio = srcProperty.tokenize( '.' ).inject( config ) { cfg, pr -> cfg[ pr ] }

我在博客上写道: http://www.baselogic.com/blog/development/grails-groovy-development/configslurper-with-dynamic-property-name-from-configurationholder-config-object

答案 2 :(得分:0)

假设类路径中有myconfig.groovy

    prompt{
     welcomeMessageOverrideGreeting {
      audio = "/en/someFileName.wav"
      txt = "Text alternative for /en/someFileName.wav"
      audio_es = "/es/promptFileName.wav"
      txt_es = "Texto alternativo para /es/someFileName.wav"
     }
    }

我们可以动态获取构建名称的属性:

def myconfig = this.class.getResource("/myconfig.groovy")
def config = new ConfigSlurper().parse(myconfig)

def dynamic = "welcomeMessageOverrideGreeting"
def dynamic2 = "audio"
def locale = "es"

assert config.prompt[dynamic].audio == "/en/someFileName.wav"
assert config.prompt.welcomeMessageOverrideGreeting[dynamic2] == "/en/someFileName.wav"
assert config.prompt.welcomeMessageOverrideGreeting["${dynamic2}_${locale}"] == "/es/promptFileName.wav"
assert config.prompt[dynamic]["${dynamic2}_${locale}"] == "/es/promptFileName.wav"