Can't call method itself in grails

橙三吉。 提交于 2019-12-13 07:38:01

问题


I got an error in my project when I try to call method it self inside body of method. I put my code in gsp.

here they are

/* 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)
        }
      }
    }       
  }
}

I use owner.call to call method itself. I got an error like this

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

Anybody can fix it?


回答1:


I put my code in gsp.

You should really put this kind of code in a taglib.

Anybody can fix it?

If this is just a standard recursive method, then the obvious way to perform the recursive call is:

createMenuChild(child1MenuInstance,paramMenuArr)

Try using this instead of

owner.call (child1MenuInstance,paramMenuArr)



回答2:


your are using a closure not a common method. see: http://groovy.codehaus.org/Closures

ownler.call means that you want to call a method named "call" of the owner (class) of the closure. mayby you can fix it by replacing owner.call by createMenuChild (child1MenuInstance,paramMenuArr). this would call the closure with the given params.




回答3:


The trick here is to predefine the closure name before assigning it.

def createMenuChild
createMenuChild = {...}

instead of

def createMenuChild = createMenuChild = {...}

Then you'll be able to reference the closure instead of invoking owner.call.



来源:https://stackoverflow.com/questions/6122626/cant-call-method-itself-in-grails

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!