Grails 3.2.6-修改BootStrap.groovy中的控制器呈现方法
当前正在将应用程序从Grails2.4.5迁移到Grails3.2.6。我们需要在呈现之前修改模板路径的能力,并且已经在Grails 2 BootStrap.groovy中这样做了:Grails 3.2.6-修改BootStrap.groovy中的控制器呈现方法,grails,grails-controller,grails-3.0,grails3.2.0,Grails,Grails Controller,Grails 3.0,Grails3.2.0,当前正在将应用程序从Grails2.4.5迁移到Grails3.2.6。我们需要在呈现之前修改模板路径的能力,并且已经在Grails 2 BootStrap.groovy中这样做了: def init = { servletContext -> log.debug "Executing BootStrap init" //Modify all controllers to work for multiple tenants. grailsApplication.co
def init = { servletContext ->
log.debug "Executing BootStrap init"
//Modify all controllers to work for multiple tenants.
grailsApplication.controllerClasses.each() { controllerClass ->
log.debug "Modifying render method on controller ${controllerClass.name}"
def oldRender = controllerClass.metaClass.pickMethod("render", [Map] as Class[])
controllerClass.metaClass.render = { Map params ->
log.debug "In bootstrap overridden method: "
if (params.template && params.template.startsWith("/common")) {
params.template = "/tenants" + params.template
log.debug "Common template found " + params.template
}
else if (params.view && params.view.startsWith("/common")) {
params.view = "/tenants" + params.view
log.debug "Common view found " + params.view
}
else if (session.tenant) {
if (params.template && params.template[0] == '/') {
log.debug "Template was " + params.template
params.template = "/tenants/" + session.tenant + params.template
log.debug "Template is " + params.template
}
if (params.view && params.view[0] == '/') {
log.debug "View was " + params.view
params.view = "/tenants/" + session.tenant + params.view
log.debug "View is " + params.view
}
}
oldRender.invoke(delegate, [params] as Object[])
}
}
}
但是,Grails3.2.6中似乎从未调用过修改后的渲染方法。有什么建议吗?如果有更好的解决方案,我愿意以其他方式重写render方法 我想你可以用拦截器来完成你的目标。请参阅Grails指南的Web层一章,我已经有了一个拦截器。但它不会在模板渲染时触发。您是否尝试过重写拦截器中的render()方法?(拦截器继承自trait-responsenderer)我会尝试将其转化为扩展responsenderer的trait,并使用traits的超级功能调用原始方法james-Duh!就是这么做的,效果很好。喜欢新的特质。