我必须知道grails渲染时的视图文件.一种方法是在过滤器中使用grails afterView动作.在这里,我找不到一种方法来知道哪个视图文件已被渲染.那么,有什么方法可以让我知道哪个视图文件已经被渲染方法渲染了?
这不是很好,但它应该适用于大多数情况.在任一使用这个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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
你需要这些进口:
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
import org.codehaus.groovy.grails.web.util.WebUtils
import org.springframework.web.context.request.RequestContextHolder
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1244 次 |
| 最近记录: |