2011-05-24 44 views
0

嗨夥計 我在我的bootstrap.groovy中有一張地圖,如何使用此地圖在我的域類中填充另一個地圖? 這裏是代碼如何在grails中的域類中填充列表?

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() 
} 

任何想法,將不勝感激

回答

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. 
    } 
}