我正在覆盖我编写的每个 Controller 的重定向方法。我这样做是因为
a)在实际执行重定向之前,我需要设置一些变量
b)我想在全局范围内这样做,因为每个 Controller 的代码都相同,所以我想干
我在 bootstrap 中执行了此操作,因为它是在服务器启动时执行的。
码:
grailsApplication.controllerClasses.each() { controllerClass ->
if (controllerClass.fullName.startsWith("my.package")){
def oldRedirect = controllerClass.metaClass.pickMethod("redirect", [Map] as Class[])
controllerClass.metaClass.redirect = { Map args ->
// pre-redirect logic
args.put("some", "property")
oldRedirect.invoke delegate, args
// post-redirect logic
}
}
}
这是问题所在:
当我进行开发时,我使用了重载功能/即时编译器,它无需重新启动服务器即可重新编译文件。在这种情况下,将重建 Controller ,结果将丢失覆盖的重定向方法。
这是问题:
是否有回调/另一种方法来查找何时在运行时重新编译类?我很想检查 Controller 是否已重建,然后再次覆盖重定向方法!
最佳答案
是的,将此功能分解为一个插件,然后您可以观察类的重新加载并重新应用您的更改。 documentation详细介绍了参与自动重载事件。
这将是完成您要完成的工作的最干净,最正确的方法。
关于grails - 在运行时重新编译 Controller 时,grails回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36575720/