grails覆盖重定向控制器方法 [英] grails override redirect controller method

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

问题描述

我试图重写默认的控制器重定向方法,但似乎无法获得以下代码工作。



我创建了一个插件,我是尝试使用doWithDynamicMethods来取代重定向。

  def doWithDynamicMethods = {ctx  - > 
application.controllerClasses.each(){controllerClass - >
replaceRedirectMethod(controllerClass)
}
}

void replaceRedirectMethod(controllerClass){
def oldRedirect = controllerClass.metaClass.pickMethod(redirect,[ Map] as Class [])
controllerClass.metaClass.redirect = {Map args,Map params - >
//永远不会到达这里


code>

我的签名是否有错,或者我错过了什么?我这样做的原因是我想改变重定向的URI如果满足某些条件,但与日志/打印语句我看到它是在应用程序启动/编译时replaceRedirectMethod,但它doesn当应用程序启动时,通过控制器进行重定向时,不会进入那里。

解决方案

是的,签名是错误的 - 重定向需要一个 Map 参数(请参阅 org.codehaus.groovy.grails.plugins.web.ControllersGrailsPlugin.registerControllerMethods()
$ p



$ p $ controllerClass。 metaClass.redirect = {Map args - >
//预重定向逻辑
oldRedirect.invoke委托,参数
//后重定向逻辑
}


I am trying to override the default controller redirect method and cannot seem to get the following bit of code to work.

I have created a plugin and I'm trying to use the "doWithDynamicMethods" to replace the redirect.

def doWithDynamicMethods = {ctx ->
   application.controllerClasses.each() { controllerClass ->
      replaceRedirectMethod(controllerClass)
   }
}

void replaceRedirectMethod(controllerClass) {
   def oldRedirect = controllerClass.metaClass.pickMethod("redirect", [Map] as Class[])
   controllerClass.metaClass.redirect = { Map args, Map params ->
      // never seems to get here    
   }
}

Do I have the signature wrong or am I missing something? The reason I'm doing this is I'd like to change the uri of the redirect if a certain condition is met but with logging/print statements I see that it is going in the "replaceRedirectMethod" upon application startup/compile but it doesn't go in there when doing a redirect via the controller once the app is started.

解决方案

Yes, the signature is wrong - redirect takes a single Map parameter (see the declaration in org.codehaus.groovy.grails.plugins.web.ControllersGrailsPlugin.registerControllerMethods())

So it should be

controllerClass.metaClass.redirect = { Map args ->
   // pre-redirect logic
   oldRedirect.invoke delegate, args
   // post-redirect logic
}

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

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