2015-04-05 39 views
0

我想通过URL参数更改配置,并尝试如下。GString错误 - 如何在程序[Grails]中使用URL参数

在控制器Config.groovy中

class TestController { 
    def grailsApplication 
    def changeConfig{ 
    Map testConfig = grailsApplication.config.test 
    def accountConfig = testConfig.${params.account} 
    } 
} 

test { 
    'default' { 
    debug  = false 
    Key  = 'aaa' 
    } 
    'another' { 
    debug  = true 
    Key  = 'bbb' 
    } 
} 

然后,我想通过改变配置运行网址,如下所示

http://localhost/myApp/test/changeConfig?account=another 

但是,此代码像下面的错误。

Class groovy.lang.MissingMethodException 
Message No signature of method: groovy.util.ConfigObject.$() is applicable for argument types: 

如何通过URL参数更改配置?

+0

你需要把$ {}双引号内 “” 在常规类。 – Ramsharan 2015-04-05 12:03:16

+0

我明白了,谢谢。 – SpaceNet 2015-05-06 11:46:52

回答

3

不知道它会工作,但你行

def accountConfig = testConfig.${params.account} 

是错误的,它应该是

def accountConfig = testConfig."${params.account}" 
+0

它的工作原理,谢谢。 – SpaceNet 2015-05-06 11:46:14

1

你可以把ConfigObject作为地图。所以你也可以这样做。

Map testConfig = grailsApplication.config.test 
def accountConfig = testConfig[params.account] 

或者

def accountConfig = testConfig.get(params.account) 
+0

它的工作原理,谢谢。 – SpaceNet 2015-05-06 11:46:18

相关问题