2015-04-05 34 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

相關問題