如何在grails的域类中填充List?

时间:2011-05-24 03:28:49

标签: groovy

嗨伙计们 我的bootstrap.groovy中有一张地图,如何使用此地图填充我的域类中的另一张地图? 这是代码

BootStrap.groovy中

def data = [ x:'45.5',
             y:'7',
             z:[ z0:'2.5', z1:'3.5', z2:'4.0', z3:'3.5', z4:'5.0']
           ]

what code should I write here?

//some code

域类

class target implements Serializable{
  //Just for the time being
  List list = new ArrayList()
}

任何想法都将不胜感激

2 个答案:

答案 0 :(得分:1)

首先,您的问题是如何使用其他地图填充一个地图,但您在域中定义了一个列表。因此,如果我正确理解您,您的域名更可能是:

class Target implements Serializable {
    Map data = [:]
}

// BootStrap.groovy
import package.name.Target
class BootStrap {

    def grailsApplication

    def init = { servletContext ->

        // no need to quote your map keys in this case
        Map data = [x:"45.5", y:"7", z:[z0 :"2.5", z1:"3.5", z2:"4.0", z3:"3.5", z4:"5.0"]

        Target targ = new Target()
        targ.data = data.z // set Target data map to nested portion of map above
        targ.data = data // set equal (could add to ctor [data:"$data"] instead)
        data.each{k,v->
            // do some calc that changes local map values and applies to target data map
        }

        // if you are unable to get a reference to Target domain, you can try
        def inst = grailsApplication.getClassForName("package.name.Target").newInstance()
        inst.data = data
        // etc.
    }
}

答案 1 :(得分:0)

我认为最好使用Grails configuration并更新Config.groovy。