我正在使用Grails 2.1.0,并被这2岁的帖子撞了一下:Is there a way to make functions accessible across controllers in Grails?
Burt建议在该处创建一个功能,所有使用Bootstrap的 Controller 都可以使用。我已经尝试过使用它的发现,但是每次我修改 Controller (添加或删除您可能认为的任何内容)时,Grails都会为我提供:
No signature of method: mycontroller.myfunctionfrombootstrap() is applicable for argument types: () values: []
这是Grails 2.1.0的“功能”吗?我想念什么吗?
不能升级Grails。
在 bootstrap 中,我有:
for (controllerClass in grailsApplication.controllerClasses) {
controllerClass.clazz.metaClass.myfunctionfrombootstrap= {
return stuff;
}
}
最佳答案
在开发模式下修改 Controller 时,Grails会对其进行重新编译,并在新类中插入插槽以替换以前的版本。但是 bootstrap 不会再次运行,因此新类不会将您的额外功能添加到其元类中。
而不是使用元类,您应该考虑将可重用的逻辑放入服务或taglib中。
关于twitter-bootstrap - Bootstrap 中的Grails全局函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28485355/