GString 错误 - 如何在程序 [Grails] 中使用 URL 参数

GString Error - how to use URL parameter in program [Grails]

我想通过 URL 参数更改配置并尝试如下。

在控制器中

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

在Config.groovy

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

然后,我想通过 运行 和 URL 更改配置,如下所示

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 参数更改配置?

不确定是否有效,但您的线路

def accountConfig = testConfig.${params.account}

写错了,应该是

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

您可以将 ConfigObject 视为地图。所以你也可以这样做。

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

 def accountConfig = testConfig.get(params.account)