【问题标题】:Can't call method itself in grails不能在grails中调用方法本身
【发布时间】:2011-05-25 10:00:03
【问题描述】:

当我尝试在方法体中调用它自己的方法时,我的项目中出现错误。我把我的代码放在 gsp 中。

他们来了

/* Method for appending the child menu */
def createMenuChild = { obj , paramMenuArr ->
  def urlChildMenu=obj.menu.url
  def idChildMenu=obj.menu.id
  def nameChildMenu=obj.menu.name

  out << '<div><a href="'+urlChildMenu+'" class="mChld">'<< nameChildMenu<< '</div>'

  def childInstance1= Menu.findById(idChildMenu)
  def child1MenuInstance= Menu.createCriteria().list{
    eq("parentMenu",childInstance1)
    order("sequence", "asc")
  }                                     
  if (child1MenuInstance){
    child1MenuInstance.each {newIt5 ->
      def idChildMenu2=newIt5.id
      paramMenuArr.each { newIt6 -> 
        if (newIt6.menu.id == idChildMenu2){
          owner.call (child1MenuInstance,paramMenuArr)
        }
      }
    }       
  }
}

我使用 owner.call 来调用方法本身。我遇到了这样的错误

Exception Message: No signature of method: bla.....

谁能解决?

【问题讨论】:

    标签: grails groovy gsp


    【解决方案1】:

    我把我的代码放在 gsp 中。

    你真的应该把这种代码放在 taglib 中。

    谁能解决?

    如果这只是一个标准的递归方法,那么执行递归调用的明显方法是:

    createMenuChild(child1MenuInstance,paramMenuArr)
    

    试着用这个代替

    owner.call (child1MenuInstance,paramMenuArr)
    

    【讨论】:

    • 我还是做不到。它总是出错..并且错误仍然像以前一样.. :(
    【解决方案2】:

    您正在使用闭包而不是常用方法。见:http://groovy.codehaus.org/Closures

    ownler.call 表示你要调用闭包的所有者(类)的一个名为“call”的方法。也许你可以通过用 createMenuChild (child1MenuInstance,paramMenuArr) 替换 owner.call 来修复它。这将使用给定的参数调用闭包。

    【讨论】:

    • 我还是做不到。它总是出错..并且错误仍然像以前一样.. :(
    • 请发布确切的异常消息或整个堆栈跟踪。
    【解决方案3】:

    这里的技巧是在分配之前预定义闭包名称。

    def createMenuChild
    createMenuChild = {...}
    

    而不是

    def createMenuChild = createMenuChild = {...}
    

    然后您将能够引用闭包而不是调用 owner.call。

    【讨论】:

      猜你喜欢
      • 2015-02-11
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多