将方法添加到控制器 [英] Add methods to controllers

查看:132
本文介绍了将方法添加到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Grails应用程序中,我想为所有控制器类添加一个foo()方法。我知道我可以在插件的doWithDynamicMethods闭包中使用如下代码来完成此操作:

  application.controllerClasses.toList()*。metaClass不过,我不想创建插件只是为了这个目的。有没有其他地方我可以做到这一点。我怀疑可能在 BootStrap.groovy  init 关闭中可能,但我不知道如何获取访问此闭包中的 GrailsApplication 实例。



谢谢,
Don

解决方案

  def grailsApplication = org.codehaus.groovy.grails.commons.ApplicationHolder.application 


In a Grails application I would like to add a foo() method to all my controller classes. I know that I can do this inside a plugin's doWithDynamicMethods closure using code like:

application.controllerClasses.toList()*.metaClass*.foo = { println 'foo called' }

However, I don't want to create a plugin just for this purpose. Is there anywhere else I can do this. I suspect it might be possible within the init closure of BootStrap.groovy, but I don't know how to get access to the GrailsApplication instance in this closure.

Thanks, Don

解决方案

def grailsApplication = org.codehaus.groovy.grails.commons.ApplicationHolder.application

这篇关于将方法添加到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆