gpt4 book ai didi

grails - 如何检测在 grails 中渲染了哪个 View 文件

转载 作者:行者123 更新时间:2023-12-04 23:22:47 25 4
gpt4 key购买 nike

当 View 文件由 grails 呈现时,我必须知道它。一种方法是过滤器中的 grails afterView 操作。在这里,我找不到一种方法来知道哪个 View 文件已被渲染。
那么,有没有什么方法可以让我知道渲染方法在实际中渲染了哪个 View 文件?

最佳答案

这不是很好,但它应该在大多数情况下工作。在 after 中使用它或 afterView :

String viewName
def webRequest = RequestContextHolder.currentRequestAttributes()
if (webRequest.renderView) {
def controller = request.getAttribute(GrailsApplicationAttributes.CONTROLLER)
def modelAndView = getModelAndView()
if (!modelAndView && controller) {
modelAndView = controller.modelAndView
}
if (modelAndView) {
viewName = modelAndView.viewName
}
else if (controller) {
// no ModelAndView, so infer from controller and action
viewName = '/' + controllerName + '/' + actionName
}
else {
// no controller, direct GSP
String uri = webRequest.attributes.urlHelper.getPathWithinApplication(request)
for (info in WebUtils.lookupUrlMappings(servletContext).matchAll(uri)) {
if (info?.viewName) {
viewName = info.viewName
break
}
}
}
}

您将需要这些导入:
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
import org.codehaus.groovy.grails.web.util.WebUtils
import org.springframework.web.context.request.RequestContextHolder

关于grails - 如何检测在 grails 中渲染了哪个 View 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19592838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com