Grails有表单提交问题

时间:2011-06-09 07:02:10

标签: grails grails-domain-class grails-controller

渲染参数并获取我提交的所有数据

["address.address1":"reston1", "address":["address1":"reston1", "county":"fairfax", "zipcode":"20190", "address2":"reston2", "city":"reston"], "representative.telephone":"7702438000", "representative":["telephone":"7702438000", "fax":"7702438001"], "address.county":"fairfax", "address.zipcode":"20190", "user.lastName":"Chinthalapudi", "user":["lastName":"Chinthalapudi", "password":"spring", "email":"srinv9@gmail.com", "firstName":"Srinivas", "userName":"srisris"], "address.address2":"reston2", "create":"Create", "user.password":"spring", "user.email":"srinv9@gmail.com", "representative.fax":"7702438001", "user.firstName":"Srinivas", "user.userName":"srisris", "address.city":"reston", "company.companyName":"ATS", "company":["companyName":"ATS"], "action":"register", "controller":"employerRegistration"]

错误堆栈

Field error in object 'com.srisris.wr.User' on field 'email': rejected value [null]; codes [com.srisris.wr.User.email.nullable.error.com.srisris.wr.User.email,com.srisris.wr.User.email.nullable.error.email,com.srisris.wr.User.email.nullable.error.java.lang.String,com.srisris.wr.User.email.nullable.error,user.email.nullable.error.com.srisris.wr.User.email,user.email.nullable.error.email,user.email.nullable.error.java.lang.String,user.email.nullable.error,com.srisris.wr.User.email.nullable.com.srisris.wr.User.email,com.srisris.wr.User.email.nullable.email,com.srisris.wr.User.email.nullable.java.lang.String,com.srisris.wr.User.email.nullable,user.email.nullable.com.srisris.wr.User.email,user.email.nullable.email,user.email.nullable.java.lang.String,user.email.nullable,nullable.com.srisris.wr.User.email,nullable.email,nullable.java.lang.String,nullable]; arguments [email,class com.srisris.wr.User]; default message [Property [{0}] of class [{1}] cannot be null]Field error in object 'com.srisris.wr.User' on field 'firstName': rejected value [null]; codes [com.srisris.wr.User.firstName.nullable.error.com.srisris.wr.User.firstName,com.srisris.wr.User.firstName.nullable.error.firstName,com.srisris.wr.User.firstName.nullable.error.java.lang.String,com.srisris.wr.User.firstName.nullable.error,user.firstName.nullable.error.com.srisris.wr.User.firstName,user.firstName.nullable.error.firstName,user.firstName.nullable.error.java.lang.String,user.firstName.nullable.error,com.srisris.wr.User.firstName.nullable.com.srisris.wr.User.firstName,com.srisris.wr.User.firstName.nullable.firstName,com.srisris.wr.User.firstName.nullable.java.lang.String,com.srisris.wr.User.firstName.nullable,user.firstName.nullable.com.srisris.wr.User.firstName,user.firstName.nullable.firstName,user.firstName.nullable.java.lang.String,user.firstName.nullable,nullable.com.srisris.wr.User.firstName,nullable.firstName,nullable.java.lang.String,nullable]; arguments [firstName,class com.srisris.wr.User]; default message [Property [{0}] of class [{1}] cannot be null]Field error in object 'com.srisris.wr.User' on field 'lastName': rejected value [null]; codes [com.srisris.wr.User.lastName.nullable.error.com.srisris.wr.User.lastName,com.srisris.wr.User.lastName.nullable.error.lastName,com.srisris.wr.User.lastName.nullable.error.java.lang.String,com.srisris.wr.User.lastName.nullable.error,user.lastName.nullable.error.com.srisris.wr.User.lastName,user.lastName.nullable.error.lastName,user.lastName.nullable.error.java.lang.String,user.lastName.nullable.error,com.srisris.wr.User.lastName.nullable.com.srisris.wr.User.lastName,com.srisris.wr.User.lastName.nullable.lastName,com.srisris.wr.User.lastName.nullable.java.lang.String,com.srisris.wr.User.lastName.nullable,user.lastName.nullable.com.srisris.wr.User.lastName,user.lastName.nullable.lastName,user.lastName.nullable.java.lang.String,user.lastName.nullable,nullable.com.srisris.wr.User.lastName,nullable.lastName,nullable.java.lang.String,nullable]; arguments [lastName,class com.srisris.wr.User]; default message [Property [{0}] of class [{1}] cannot be null]Field error in object 'com.srisris.wr.User' on field 'password': rejected value [null]; codes [com.srisris.wr.User.password.nullable.error.com.srisris.wr.User.password,com.srisris.wr.User.password.nullable.error.password,com.srisris.wr.User.password.nullable.error.java.lang.String,com.srisris.wr.User.password.nullable.error,user.password.nullable.error.com.srisris.wr.User.password,user.password.nullable.error.password,user.password.nullable.error.java.lang.String,user.password.nullable.error,com.srisris.wr.User.password.nullable.com.srisris.wr.User.password,com.srisris.wr.User.password.nullable.password,com.srisris.wr.User.password.nullable.java.lang.String,com.srisris.wr.User.password.nullable,user.password.nullable.com.srisris.wr.User.password,user.password.nullable.password,user.password.nullable.java.lang.String,user.password.nullable,nullable.com.srisris.wr.User.password,nullable.password,nullable.java.lang.String,nullable]; arguments [password,class com.srisris.wr.User]; default message [Property [{0}] of class [{1}] cannot be null]Field error in object 'com.srisris.wr.User' on field 'userName': rejected value [null]; codes [com.srisris.wr.User.userName.nullable.error.com.srisris.wr.User.userName,com.srisris.wr.User.userName.nullable.error.userName,com.srisris.wr.User.userName.nullable.error.java.lang.String,com.srisris.wr.User.userName.nullable.error,user.userName.nullable.error.com.srisris.wr.User.userName,user.userName.nullable.error.userName,user.userName.nullable.error.java.lang.String,user.userName.nullable.error,com.srisris.wr.User.userName.nullable.com.srisris.wr.User.userName,com.srisris.wr.User.userName.nullable.userName,com.srisris.wr.User.userName.nullable.java.lang.String,com.srisris.wr.User.userName.nullable,user.userName.nullable.com.srisris.wr.User.userName,user.userName.nullable.userName,user.userName.nullable.java.lang.String,user.userName.nullable,nullable.com.srisris.wr.User.userName,nullable.userName,nullable.java.lang.String,nullable]; arguments [userName,class com.srisris.wr.User]; default message [Property [{0}] of class [{1}] cannot be null]

我的域类

package com.srisris.wr

class User {

    String userName
    String password
    //String passwordConfirm
    String firstName
    String lastName
    String email

    static mapping = {
        tablePerHierarchy false
    }
    static constraints = {

    }
}

包com.srisris.wr

class Representative extends User{
    static belongsTo = [ company: Company ]



    String jobTitle
    String telephone
    String fax

    static constraints = {

    }
}


package com.srisris.wr

class Employer extends User {
     Address address

    static constraints = {

    }

}

package com.srisris.wr

class Employee extends User{
     Address address

    static constraints = {

    }
}


package com.srisris.wr

class Company {

    String companyName
    Address address
    String companyRegistration
    int totalEmployeeStrength
    int numberOfUkPAYEEmployees
    String category
    Representative representative
    Bank bank



    static constraints = {

    }
}


package com.srisris.wr

class Bank {
    static belongsTo = [ company: Company ]
   // static belongsTo = [ retailer: Retailer ]
    Company company

    static constraints = {

    }
}


package com.srisris.wr

class Address {

   static belongsTo = [ user: User,company: Company ]
    //static belongsTo = [ company: Company]
    User user
    String address1
    String address2
    String city
    //String state
    String county
    String zipcode

    Company company

    static constraints = {

    }
}

我的控制器

包com.srisris.wr.register.employer     import com.srisris.wr.User

class EmployerRegistrationController {

    def index = { }

    def register={
        render params

        User user= new User(params)
        user.save()

        if(user.hasErrors()){
            user.errors.allErrors.each {
                render it
            }

        }
    }
}

我的GSP

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl">
  <head>
    <title>Workriders - Employer Registration page - JustProto</title>
  </head>
  <body>
    <g:form action="register" method="post" >

        <table width="80%" height="60%" align="left">
          <tr><th align="right"><g:message code="Your Company Name"/></th><td><g:textField name="company.companyName"></g:textField></td></tr>
          <tr><th align="right"><g:message code="Address"/></th> <td><g:textField name="address.address1" ></g:textField></td></tr><tr><td>&nbsp;</td><td><g:textField name="address.address2"></g:textField></td></tr>
          <tr><th align="right"><g:message code="Town/City"/></th><td><g:textField name="address.city" ></g:textField></td></tr>
          <tr><th align="right"><g:message code="County"/></th><td><g:textField name="address.county" ></g:textField></td></tr>
          <tr><th align="right"><g:message code="PostCode"/></th><td><g:textField name="address.zipcode" ></g:textField></td></tr>
          <tr><th align="right"><g:message code="First Name"/></th><td align="left"><g:textField name="user.firstName"></g:textField></td></tr>
          <tr><th align="right"><g:message code="Last Name"/></th><td><g:textField name="user.lastName"></g:textField></td></tr>
          <tr><th align="right"><g:message code="Telephone"/></th> <td><g:textField name="representative.telephone" ></g:textField></td></tr>
          <tr><th align="right"><g:message code="Fax"/></th><td><g:textField name="representative.fax" ></g:textField></td></tr>
          <tr><th align="right"><g:message code="Email"/></th><td><g:textField name="user.email" ></g:textField></td></tr>
          <tr><th align="right"><g:message code="User Name"/></th><td><g:textField name="user.userName"></g:textField></td></tr>
          <tr><th align="right"><g:message code="password"/></th><td><g:passwordField name="user.password" ></g:passwordField></td></tr>

          <tr><td>&nbsp;</td><td> <g:submitButton name="create" value="Create"/></td></tr>          
        </table>
      </div>
    </g:form>

  </body>

</html>

2 个答案:

答案 0 :(得分:2)

当堆栈跟踪指出时,发生该错误是因为用户的email字段为空。

这意味着User user= new User(params)失败。那是因为你给出了错误的参数来初始化用户,例如,“用户类”没有“地址”属性。您应该new Employer()代替。

答案 1 :(得分:0)

GORM将nullable: false设置为域的每个字段的默认约束。因此,当您在域中保存一行时,由于nullable:false,您给出的错误将会解决。要解决此问题,一种方法是将nullable:true设置为GORM的默认约束。您可以在Config.groovy

中进行设置
grails.gorm.default.constraints = {
   '*'(nullable:true, blank:true)
} 

当您需要为任何字段设置nullable:true时,您将手动将其设置在域类中。