覆盖Grails重定向方法 [英] Override Grails redirect method
问题描述
我在URL中携带了我的Grails应用程序的语言环境,例如
http://myapp.com/LANG/controller/action/id 。
因此,我用语言属性调整URLMappings.groovy。因为我想保存工作并且不在每个链接上应用lang参数,所以我已经用params.lang中的语言覆盖了g:link taglib。这很好。
当我从控制器进行重定向时,比如
redirect(action:logout)
我想追加params .lang自动而不是每次都写
redirect(action:logout,params:[lang:params.lang])
我找到了线程 grails覆盖重定向控制器方法,但我不确定如何继续。
如何覆盖redirect()方法并添加params.lang-attribute?
请指教,非常感谢。
在您的应用程序中执行此操作的最简单方法是为你的Grails应用程序使用一些元编程和 BootStrap.groovy
。以下只是一个简单的例子:
import org.codehaus.groovy.grails.web.servlet .GrailsApplicationAttributes
class BootStrap {
def init = {servletContext - >
def ctx = servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT)
def app = ctx.getBean(grailsApplication)
app.controllerClasses.each(){controllerClass - >
def oldRedirect = controllerClass.metaClass.pickMethod(redirect,[Map] as Class [])
controllerClass.metaClass.redirect = {Map args - >
//新的预重定向逻辑
if(!args ['params'])args ['params'] = [:] //以防万一在没有任何参数的情况下调用重定向
args ['params'] ['whatever'] ='something'//向参数映射中添加内容
oldRedirect.invoke委托,参数
//新的重定向逻辑
$ b def destroy = {
}
}
上面的例子仅包含了Grails应用程序中所有控制器上的 redirect
的实现,并注入了一个名为任何
与的东西
。
以上是使用Grails 2.4.2测试(快速)。
I'm carrying the locale of my Grails application in the URL, such as
http://myapp.com/LANG/controller/action/id.
Therefore, I adjusted the URLMappings.groovy with the language attribute. Since I would like to save work and don't apply the lang parameter at every link, I have overridden the g:link taglib with the language from params.lang. This works pretty good.
When I do a redirect from a controller, such as
redirect(action: "logout")
I'd like to append the params.lang automatically instead of writing every time
redirect(action: "logout", params: [lang: params.lang])
I found the thread grails override redirect controller method, but I'm unsure how to proceed. Where does this code take part?
How can I override the redirect() method and add the params.lang-attribute?
Please advice, thank you very much.
The easiest way to do this in your application would be to use a bit of meta programming and the BootStrap.groovy
for your Grails application. The following is just a simple example of what that might look like:
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
class BootStrap {
def init = { servletContext ->
def ctx = servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT)
def app = ctx.getBean("grailsApplication")
app.controllerClasses.each() { controllerClass ->
def oldRedirect = controllerClass.metaClass.pickMethod("redirect", [Map] as Class[])
controllerClass.metaClass.redirect = { Map args ->
// new pre-redirect logic
if (!args['params']) args['params'] = [:] // just in case redirect was called without any parameters
args['params']['whatever'] = 'something' // add something into the parameters map
oldRedirect.invoke delegate, args
// new post-redirect logic
}
}
}
def destroy = {
}
}
The above example just wraps the implementation for redirect
on all controllers within your Grails application and injects a new parameter called whatever
with the value of something
.
The above was tested (quickly) using Grails 2.4.2.
这篇关于覆盖Grails重定向方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!