【问题标题】:GRAILS: groovy.lang.MissingPropertyException: No such property.GRAILS:groovy.lang.MissingPropertyException:没有这样的属性。
【发布时间】:2011-09-28 20:45:20
【问题描述】:

我正在尝试调试 Grails 应用程序。不幸的是,我之前没有使用上述语言的经验。

当我执行 grails generate-all org.example.Book 时,我得到一个模糊的错误:

Generating controller for domain class org.example.Book ...
groovy.lang.MissingPropertyException: No such property: Event for class: SimpleTemplateScript6
    at SimpleTemplateScript6.run(SimpleTemplateScript6.groovy:22)
    at _GrailsGenerate_groovy.generateForDomainClass(_GrailsGenerate_groovy:88)
    at _GrailsGenerate_groovy$_run_closure1.doCall(_GrailsGenerate_groovy:48)
    at GenerateAll$_run_closure1.doCall(GenerateAll.groovy:42)
    at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
    at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
    at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
    at gant.Gant.withBuildListeners(Gant.groovy:427)
    at gant.Gant.this$2$withBuildListeners(Gant.groovy)
    at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
    at gant.Gant.dispatch(Gant.groovy:415)
    at gant.Gant.this$2$dispatch(Gant.groovy)
    at gant.Gant.invokeMethod(Gant.groovy)
    at gant.Gant.executeTargets(Gant.groovy:590)
    at gant.Gant.executeTargets(Gant.groovy:589)
Error running generate-all: No such property: Event for class: SimpleTemplateScript6

查看生成的 org.example.BookController.groovy 源码后,我注意到它并没有完全生成(停在 List fields = []):

package org.example

// import needed for export plugin
import org.codehaus.groovy.grails.commons.ConfigurationHolder

class BookController {

    ...
    def exportService

    def export = {attrs ->
    def response = attrs.response
    List fields = []

这个错误似乎是由templates/scaffolding/Controller.groovy中的以下代码引起的:

<%=packageName ? "package ${packageName}\n\n" : ''%>

// import needed for export plugin
import org.codehaus.groovy.grails.commons.ConfigurationHolder

class ${className}Controller {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]^M

    def exportService

    ...

    def export = {attrs ->
    def response = attrs.response
    List fields = []
    <% excludedProps = Event.allEvents.toList() << 'version'
       allowedNames = domainClass.persistentProperties*.name << 'id' << 'dateCreated' << 'lastUpdated'
       props = domainClass.properties.findAll { allowedNames.contains(it.name) && !excludedProps.contains(it.name) && !Collection.isAssignableFrom(it.type) }
       Collections.sort(props, comparator.constructors[0].newInstance([domainClass] as Object[]))
       props.eachWithIndex {p, i -> %>fields.push("${p.name}"<%="\n"%><% } %>
       Map labels = [:]
       <% props.eachWithIndex { p, i -> %>labels.putAt("\${p.name}", "\${p.naturalName}")<%="\n "%><% } %>

       Map formatters = [:]
       Map parameters = [:]
       response.setHeader("Content-disposition", "attachment; filename=${domainClass.propertyName}s.\${attrs.extension}")
       if("\${attrs.extension}" == "xml") {
            exportService.export(attrs.format, response.outputStream, attrs.exportList, fields, [:], formatters, parameters)
       } else {
            exportService.export(attrs.format, response.outputStream, attrs.exportList, fields, labels, formatters, parameters)
       }
    }
...
}

似乎 def export {} 块似乎引起了问题。上面的代码有问题吗?

【问题讨论】:

    标签: grails groovy


    【解决方案1】:

    Event 类是什么?有导入吗?

    【讨论】:

    • 你是对的!我查了一下,结果发现我需要在 Controller.groovy 的顶部包含以下行:
    【解决方案2】:

    检查您的域类是否存在语法错误。确保在你的域类中,你已经包含了必要的包,在你的例子中,包 org.example。

    【讨论】:

      猜你喜欢
      • 2013-09-28
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多