目前正在将应用程序从Grails 2.4.5迁移到Grails 3.2.6。我们需要能够在渲染之前修改模板路径,并在Grails 2 BootStrap.groovy中完成此操作:
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[])
}
}
}
但是,Grails 3.2.6似乎永远不会调用修改后的渲染方法。有什么建议?如果有更好的解决方案,我愿意以其他方式覆盖渲染方法。 。