开发者

Can't call method itself in grails

开发者 https://www.devze.com 2023-03-08 01:44 出处:网络
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.

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?


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)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消