在grails中创建动态变量名称

时间:2013-10-17 09:08:20

标签: grails groovy grails-2.0

我遇到动态变量名称的问题

我有像

这样的表单参数
ip1
ip2
ip3
dns1
dns2
...

我知道有多少,但我不知道如何在控制器中创建变量名

def ipcount = params.count
    for (i = 1; i = ipcount ; i++ ){
         def systems = new Ip() 
         systems.inetAddress = params.ip+${i}  <---- How do I create these variable name?
                 ....

1 个答案:

答案 0 :(得分:4)

将它们用双引号括起来:

def ipcount = params.int( 'count' )
def systemsList = (1..ipcount).collect { i ->
    def systems = new Ip() 
    systems.inetAddress = params."ip${i}"
    ...
    systems
}