如何在 grails 的域类中填充列表?

发布于 2024-11-09 18:28:12 字数 483 浏览 0 评论 0原文

大家好 我的 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

domain class

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

任何想法将不胜感激

Hi folks
I have a map in my bootstrap.groovy, how can use this map to populate another map in my domain class?
here is the code

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

domain class

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

any ideas would be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧话新听 2024-11-16 18:28:12

第一件事是您的问题询问如何用另一个地图填充一个地图,但您在域中定义了一个列表。因此,如果我理解正确,您的域更有可能是:

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

First thing is your question asks how to populate one map with another map, but you define a list in your domain. So if I'm understanding you correctly, your domain would more likely be:

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.
    }
}
晒暮凉 2024-11-16 18:28:12

我相信最好使用 Grails 配置并更新 Config.groovy。

I believe it's better to use Grails configuration and update Config.groovy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文