gpt4 book ai didi

grails - 向Grails中的多个Domain对象提交表单

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

我是Grails的新手。我正在学习基本的CRUD操作。在这里,我有2个域对象Person和Address。每个人只需要一个地址

因此,我有一个gsp表单,该表单收集用户名,名字,姓氏,年龄和地址字段,并应基本上存储到数据库Person和Address中的2个表中。因此,如何在映射2个域类(人员和地址)的 Controller 中编写代码。

谢谢回答。

更新:我用下面的代码不起作用

package com.deltaintech.wr

class Person {
String username
String password
String firstname
String lastname
String email
Address address

static constraints = {
}
}


package com.deltaintech.wr

class Address {

String address1
String address2
String city
String state
String country
String zipcode

static constraints = {
}
}

package com.deltaintech.wr.register
import com.deltaintech.wr.*

class RegisterController {

def index = {

}

def register = {

Person person = new Person(params)
person.save()

}
}

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->

<%@ page contentType="text/html;charset=UTF-8" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sample title</title>
</head>
<body>
<h1>Sample line</h1>
<g:form action="register">
User Name <g:textField name="username"/><br>
Password <g:passwordField name="password" /><br>
First Name <g:textField name="firstname"/><br>
Last Name <g:textField name="lastname"/><br>
Email <g:textField name="email" /><br>
Address1 <g:textField name="address.address1"/><br>
Address2 <g:textField name="address.address2" /><br>
City <g:textField name="address.city" /><br>
State<g:textField name="address.state" /><br>
Country <g:textField name="address.country" /><br>
Zip Code <g:textField name="address.zipcode" /><br>
<g:submitButton name="create" value="Create"/>
</g:form>
</body>
</html>


Error 500: Executing action [register] of controller [com.deltaintech.wr.register.RegisterController] caused exception: not-null property references a null or transient value: com.deltaintech.wr.Person.address; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.deltaintech.wr.Person.address
Servlet: grails
URI: /system/grails/register/register.dispatch
Exception Message: not-null property references a null or transient value: com.deltaintech.wr.Person.address
Caused by: not-null property references a null or transient value: com.deltaintech.wr.Person.address
Class: RegisterController
At Line: [13]

最佳答案

class Person { 
String name
Address address
}

class Address {
String city
}
差距必须看起来像:
<g:form action="save">
<g:textField name="name"/>
<g:textField name="address.city"/>
</g:form>
在 Controller 中:
def p = new Person(params)
p.save()

关于grails - 向Grails中的多个Domain对象提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6261880/

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