嗨我有这个类(class):

class Carro {

    String name
    String marca
    String matricula

    static constraints = {
        name(nullable:false, blank:false)
    }

    static mapping = {
        version false
    }
}
class CarroMovel {
    String move
    String rodas
    String espelhos
    Carro carro

    static hasMany = [carros: Carro]

    static constraints = {
        move(nullable:false, blank:false)
    }

    static mapping = {
        version false
    }
}

和 Controller :
class CarroController{
def save2 = {

        def carroInstance = new Carro()
        carroInstance.name = params.name
        carroInstance.marca = params.marca
        carroInstance.matricula = params.matricula

        if (carroInstance.save(flush: true)) {
            redirect(uri:"/home.gsp")
        }
        else {
            render(view: "/testeAdd", model: [carroInstance: carroInstance])
        }

    }

和 View testeAdd.gsp
<g:form controller="carro" action="save2">
<h1>Add New Carro Record</h1>
<p>Basic Information</p>

<label>Name
<span class="small">as</span>
</label>
<input type="text" name="name" value="${carroInstance?.name}" /><br>

<label>Marca
<span class="small">as</span>
</label>
<input type="text" name="marca" value="${carroInstance?.marca}" /><br

  <label>Matricula
<span class="small">as</span>
</label>
<input type="text" name="matricula" value="${carroInstance?.matricula}" /><br>


<g:submitButton name="save" value="Save" id="oneone"/>


<div class="spacer"></div>
  </g:form>

                <g:hasErrors bean="${carroInstance}">
            <div class="errors">
                <g:renderErrors bean="${carroInstance}" as="list" />
            </div>
            </g:hasErrors>

这很好。现在,我希望能够对多个域类进行数据绑定(bind)。因此,连同我gsp文件中的当前代码,我还想将carroMovel事件全部添加到同一save2中。我不确定该怎么做,特别是因为类Carro将需要具有来自类carroMovel的ID。有什么帮助吗?谢谢。

我提出了一些建议,现在结果如下(我还不关心错误验证):
 def save3 = {

        def carroInstance = new Carro()
        def carroMovelInstance = new CarroMovel()

        carroInstance.name = params.carro.name
        carroInstance.marca = params.carro.marca
        carroInstance.matricula = params.carro.matricula

        carroMovelInstance.move = params.carroMovel.move
        carroMovelInstance.rodas = params.carroMovel.rodas
        carroMovelInstance.espelhos = params.carroMovel.espelhos

        carroInstance.save()
        carroMovelInstance.carro = carroInstance
        carroMovelInstance.save()

    }
<g:form controller="carro" action="save3">
<h1>Add New Conference Record</h1>
<p>Basic Information</p>

<label>Name
<span class="small">Add your name</span>
</label>
<input type="text" name="carro.name" value="${carroInstance?.name}" /><br>

<label>Marca
<span class="small">Add your name</span>
</label>
<input type="text" name="carro.marca" value="${carroInstance?.marca}" /><br

  <label>Matricula
<span class="small">Add your name</span>
</label>
<input type="text" name="carro.matricula" value="${carroInstance?.matricula}" /><br>

<label>Move
<span class="small">Add your name</span>
</label>
<input type="text" name="carroMovel.move" value="${carroMovelInstance?.move}" /><br>

<label>Rodas
<span class="small">Add your name</span>
</label>
<input type="text" name="carroMovel.rodas" value="${carroMovelInstance?.rodas}" /><br>

<label>Espelho
<span class="small">Add your name</span>
</label>
<input type="text" name="carroMovel.espelho" value="${carroMovelInstance?.espelho}" /><br>

<g:submitButton name="save" value="Save" id="addConference"/>

Carro对象保存在数据库中,高高在上,CarroMovel没有任何反应,它也没有保存,我无法弄清楚。

最佳答案

如果我正确理解了您的问题,可以尝试一下。
首先,编辑我们的表单以包含CarroMovel类的必要字段,
例如

<label>Move
<span class="small">as</span>
</label>
<input type="text" name="move" value="${carroMovelInstance?.move}" />

然后

在您的save2 Action 中,
def carroInstance = new Carro()
carroInstance.name = params.name
carroInstance.marca = params.marca
carroInstance.matricula = params.matricula

def carroMovelInstance = new CarroMovel()
carroMovelInstance.name = params.move
carroMovelInstance.marca = params.rodasa
carroMovelInstance.matricula = params.espelhos
carroMovelInstance.carro = carroInstance

由于Carro不属于CarroMovel,因此保存carroMovelInstance不会级联到carroInstance,因此您需要在保存自己的实例之前分别保存每个实例。
carroMovelInstance.carro.save()
if (carroMovelInstance.save(flush: true)) {
        redirect(uri:"/home.gsp")
    }
    else {
        render(view: "/testeAdd", model: [carroInstance: carroInstance, carroMovelInstance:carroMovelInstance])
  }

让我知道是否适合您。

08-29 00:14