【问题标题】:Grails form creation for many-to-one belongsTo多对一的 Grails 表单创建
【发布时间】:2013-11-24 06:46:58
【问题描述】:

我是grails 的初学者,我有一个基本问题。我想在子类表单创建中显示父类的实例列表。

My Domain 类如下。 Parent 类是 Company。

class Company {
    String name
    static constraints = {
        name(blank:false)
    }
    String toString(){name}
}

我的子类是公司的位置。

class Location {
    String name
    String address
    static belongsTo= {companyLocation:Company}

    static constraints = {
    name(blank: false)
    address blank:false 
}

    String toString(){"company:"+companyLocation+"Location:"+name}
}

现在在_form template' of location view I have the code for thecompanyLocation 下拉菜单中`

<div class="fieldcontain ${hasErrors(bean: locationInstance, field: 'companyLocation', 'error')} required">
<label for="companyLocation">
<g:message code="location.companyLocation.label" default="companyLocation" />
    <span class="required-indicator">*</span>
    <g:select id="companyLocation" name="companyLocation.id" from="${first_project.Company.list()}" optionKey="id" required="" value="${locationInstance?.companyLocation?.id}" class="many-to-one"/>
</label>
</div>

当我进入创建页面时出现错误:

Error 500: Internal Server Error
URI /first_project/location/create
Class  groovy.lang.MissingPropertyException
Message No such property: companyLocation for class: first_project.Location

当我在 Location Domain 类中定义了静态变量 companyLocation 时,为什么会出现此错误?有人可以告诉我哪里出错了吗?

提前致谢。

【问题讨论】:

    标签: grails grails-2.0 gsp


    【解决方案1】:

    这看起来像是语法问题,

    static belongsTo= {companyLocation:Company}
    

    应该是

    static belongsTo= [companyLocation:Company]
    

    【讨论】:

      【解决方案2】:

      还有一种 OO 方法可以做到这一点,而不是使用 has many and belongs to...

      创建另一个 CompanyLocation 域类。

      Class CompanyLocation {
         Company company
         Location location
      
      static constraints = {
         company(blank:false, nullable:false)
         location(blank:false, nullable:false)
      }
      
      public String toString() {
          return "${company} ${location}"
      }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-13
        相关资源
        最近更新 更多