问题
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