无法在 grails 中调用方法本身
当我尝试在方法体内调用方法时,我的项目中出现错误。我把我的代码放在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.....
有人可以修复它吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您确实应该将此类代码放入标记库中。
如果这只是一个标准的递归方法,那么执行递归调用的明显方法是:
尝试使用 this 而不是
You should really put this kind of code in a taglib.
If this is just a standard recursive method, then the obvious way to perform the recursive call is:
Try using this instead of
您使用的闭包不是常用方法。请参阅: http://groovy.codehaus.org/Closures
ownler.call 表示您要调用闭包所有者(类)的名为“call”的方法。也许您可以通过用createMenuChild (child1MenuInstance,paramMenuArr) 替换owner.call 来修复它。这将使用给定的参数调用闭包。
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.
这里的技巧是在分配闭包名称之前预先定义它。
而不是
Then 你将能够引用闭包而不是调用owner.call。
The trick here is to predefine the closure name before assigning it.
instead of
Then you'll be able to reference the closure instead of invoking owner.call.