如何检测在grails中呈现哪个视图文件 [英] How to detect which view file has been rendered in grails
本文介绍了如何检测在grails中呈现哪个视图文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,有没有什么方法可以让我知道render方法在渲染哪个视图文件? >这不是很好,但它应该在大多数情况下工作。在或
afterView
之后使用:
<$ p $如果(webRequest.renderView){
def控制器= request.getAttribute(GrailsApplicationAttributes.CONTROLLER)
$ def webRequest = RequestContextHolder.currentRequestAttributes()
字符串viewName $ b $ $ b def modelAndView = getModelAndView()
if(modelAndView&& controller){
modelAndView = controller.modelAndView
}
if(modelAndView){
viewName = modelAndView.viewName
else if(controller){
// no ModelAndView,所以从控制器和动作推断
viewName ='/'+ controllerName +'/' + actionName
$ {
//无控制器,直接GSP
字符串uri = webRequest.attributes.urlHelper.getPathWithinApplication(请求)
for(info in WebUtils .lookupUrlMappings(servletContext).matchAll(uri)){
if(info?.viewName){
view Name = 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
I have to know the view file when it is rendered by grails. One way is grails afterView action in filter. Here, I couldn't find a way to know which view file has been rendered. So, Is there any method through which I know which view file has been rendered by render method in action?
解决方案
This isn't pretty, but it should work in most cases. Use this in either after
or 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
}
}
}
}
You'll need these imports:
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
import org.codehaus.groovy.grails.web.util.WebUtils
import org.springframework.web.context.request.RequestContextHolder
这篇关于如何检测在grails中呈现哪个视图文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文