如何使用Grails从一个视图中保存多个对象 [英] How to save multiple object from one view using Grails

查看:77
本文介绍了如何使用Grails从一个视图中保存多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题跟在这篇文章之后 Grails一对多关系视图

This question is in follow up to this post Grails one to many relationship view

这个例子显示没有工作,并且在运行时抛出以下异常

The example suggested there is not working and throwing following exception at run time

null id in blog.omarello.Phone entry (don't flush the Session after an exception occurs). Stacktrace follows:
Message: null id in blog.omarello.Phone entry (don't flush the Session after an exception occurs)
   Line | Method
->>  43 | doCall  in blog.omarello.ContactController$_closure4$$ENLORkU6
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run . . in     ''
^   662 | run     in java.lang.Thread

我认为宁可让示例工作,任何人都可以帮助我理解我如何创建一个可以让我保存同一个域类的多个实例的GSP。例如,一个GSP可以让我一次插入多个Book实例?

I think rather then making the example work can any one help me understand how can I create a GSP which can let me save multiple instances of same domain class. For example, a GSP which can let me insert multiple Book instances at once?

推荐答案

将手机示例中的Contact类更改为

Change the Contact class in the phone example as follows and it should work fine.

package blog.omarello

import org.apache.commons.collections.list.LazyList;
import org.apache.commons.collections.FactoryUtils;

class Contact {

    static constraints = {
        firstName(blank:false)
        lastName(blank:false)
    }

    String firstName
    String lastName
    String nickName


 List phones = LazyList.decorate(new ArrayList(),
                                  FactoryUtils.instantiateFactory(Phone.class));


//    List phones = new ArrayList()
    static hasMany = [ phones:Phone ]

    static mapping = {
        phones cascade:"all-delete-orphan"
    }

//    def getPhonesList() {
//        return LazyList.decorate(
//              phones,
//              FactoryUtils.instantiateFactory(Phone.class))
//    }

    def String toString() {
        return "${lastName}, ${firstName}"
    }
}

这篇关于如何使用Grails从一个视图中保存多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆