0

目前正在将应用程序从 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[])
        }
    }

}

然而,修改后的 render 方法似乎从未在 Grails 3.2.6 中被调用。有什么建议么?如果有更好的解决方案,我愿意以其他方式覆盖渲染方法。. .

4

0 回答 0