问题描述
我试图重写默认的控制器重定向方法,但似乎无法获得以下代码工作。
我创建了一个插件,我是尝试使用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覆盖重定向控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!