gpt4 book ai didi

Grails 数据绑定(bind)多个域类

转载 作者:行者123 更新时间:2023-12-02 14:04:16 25 4
gpt4 key购买 nike

你好。我有这个类(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 文件中的当前代码,我还想在同一个 save2 中添加 carroMovel 事件。我不知道该怎么做,特别是因为 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 没有任何 react ,而且它没有保存,我无法弄清楚。

最佳答案

首先,我会将输入名称更改为 carro.name、carro.marca、carroMovel.move,... 以便按名称区分它们。

<input type="text" name="carro.name"/><br>
<input type="text" name="carro.marca"/><br>
<input type="text" name="carroMovel.move"/><br>

这样做的好处是 Controller 中的绑定(bind)可以通过标准的 Grails 方式完成,并且可以在没有设置 value 属性的情况下在表单中输入正确的值。
carro.properties = params.carro 
carroMovel.properties = params.carroMovel

在 Controller Action 中,您还可以保存和链接 Carro 和 CarroMovel 实例。
carro.save()
carroMovel.carro = carro
carroMovel.save()
if(carroMovel.hasErrors(){
render(view: 'save3', model: [carro: carro, carroMovel.carroMovel])
}

关于Grails 数据绑定(bind)多个域类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6038378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com