开发者

Form Submission to multiple Domain objects in Grails

开发者 https://www.devze.com 2023-03-11 23:11 出处:网络
I am very much a newbie to Grails. I am learning a basic CRUD operation. Here I have 2 domain object Person and Address. Each Person only needs to have only one Address

I am very much a newbie to Grails. I am learning a basic CRUD operation. Here I have 2 domain object Person and Address. Each Person only needs to have only one Address

So I have a gsp form which collects username, first name, last name, age and address fields and should store into basically 2 tables in database Person and Address. So how write the code in the controllers which maps 2 domain classes(Person & Address).

Thanks for answering.

UPDATE: I used the following code which didnt work

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 
} 

the gsp has to look somethink like:

<g:form action="save">
    <g:textField name="name"/>
    <g:textField name="address.city"/>
</g:form>

in the controller:

def p = new Person(params)
p.save()
0

精彩评论

暂无评论...
验证码 换一张
取 消