`
bloodwolf_china
  • 浏览: 129753 次
社区版块
存档分类
最新评论

grails的requestDispatcher的forward问题

阅读更多
java web开发关于请求的处理转发有两种方式,
  
  • 一是redirect ,返回302状态和一个新Location给浏览器,让浏览器重新请求
  •    二是requestDispatcher,可以用forward方法把请求转发给目标处理器。
  • grails 1.03现在的controller只提供了redirect方法,没有提供forward方法。
    即使自己使用request.getRequestDispatcher("/controller/action").forward(request,response)方法或在Controller中返回ModelAndView("forward:/controller/action"),想让grails转发请求,grails也不支持,会得到404错误。
   web 2.4规范filter对请求的处理方式REQUEST(外部请求)、FORWARD(内部转发)、INCLUDE(包含) 和 ERROR(错误),grails的URLMapping Filter只处理REQUEST,所以就处理不到forward的请求。
   解决方法:   [list]
  
  • 1:修改grails源码和web.xml对filter配置,不过grails的jira中已经宣称在1.1版本中会在Controller中支持forward方法
  •    [*]2:看看grails的web.xml配置,所有的.dispatch结尾的请求会有GrailsDispatcherServlet来处理,在看看grails的源码
    public class UrlMappingsFilter extends OncePerRequestFilter {
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    ...
    if(viewName == null || viewName.endsWith(GSP_SUFFIX) || viewName.endsWith(JSP_SUFFIX)) {
                                String forwardUrl = WebUtils.forwardRequestForUrlMappingInfo(request, response, info);
                                if(LOG.isDebugEnabled()) {
                                    LOG.debug("Matched URI ["+uri+"] to URL mapping ["+info+"], forwarding to ["+forwardUrl+"] with response ["+response.getClass()+"]");
                                }
    
                            }
    }
    }
    
    public class WebUtils extends org.springframework.web.util.WebUtils {
    
    		...
    		
    		public static String forwardRequestForUrlMappingInfo(HttpServletRequest request, HttpServletResponse response, UrlMappingInfo info) throws ServletException, IOException {
            return forwardRequestForUrlMappingInfo(request, response, info, Collections.EMPTY_MAP);
        }
    
        public static String forwardRequestForUrlMappingInfo(HttpServletRequest request, HttpServletResponse response, UrlMappingInfo info, Map model) throws ServletException, IOException {
            GrailsWebRequest webRequest = (GrailsWebRequest) RequestContextHolder.currentRequestAttributes();
            String forwardUrl = buildDispatchUrlForMapping(info);
    
            //populateParamsForMapping(info);
            RequestDispatcher dispatcher = request.getRequestDispatcher(forwardUrl);
            populateWebRequestWithInfo(webRequest, info);
    
            exposeForwardRequestAttributes(request);
            exposeRequestAttributes(request, model);
            dispatcher.forward(request, response);
            return forwardUrl;
        }
        
        public static String buildDispatchUrlForMapping(UrlMappingInfo info) {
            final StringBuffer forwardUrl = new StringBuffer();
    
            if (info.getViewName() != null) {
                String viewName = info.getViewName();
                forwardUrl.append(SLASH).append(viewName);
            }
            else {
            		// GRAILS_SERVLET_PATH="/grails" SLASH='/' GRAILS_DISPATCH_EXTENSION=".dispatch"
                forwardUrl.append(GrailsUrlPathHelper.GRAILS_SERVLET_PATH);
                forwardUrl.append(SLASH)
                                  .append(info.getControllerName());
    
                if(!StringUtils.isBlank(info.getActionName())) {
                    forwardUrl.append(SLASH)
                              .append(info.getActionName());
                }
                forwardUrl.append(GrailsUrlPathHelper.GRAILS_DISPATCH_EXTENSION);
            }
    
            return forwardUrl.toString();
        }
    }
    
    按照grails处理规则,只要forward的url为/grails/controller/action.dispatch,grails就可以处理了。
      [/list]
       所以暂时的解决方法就是返回一个ModelAndView(forwardurl),forwardurl的格式为"forward:/grails/${controllerName}/${actionName}.dispatch"
       
    分享到:
    评论

    相关推荐

      Grails Grails Grails

      Grails Grails Grails Grails Grails

      grails技术应用问题总汇

      这个是我在开发中用到的技术,很多都是第一次使用。

      Grails权威指南 Grails权威指南

      Grails权威指南Grails权威指南Grails权威指南Grails权威指南Grails权威指南Grails权威指南

      Eclipse下搭建Grails项目

      Grails项目的应用越来越多,而对于初学者来说,在Eclipse下搭建Grails项目是一个难题,这个文档将教会你如何搭建Grails项目,希望对你有所帮助。

      Grails入门指南 -- 针对grails1.0.4更新

      Grails入门指南中文pdf -- 针对grails1.0.4更新,附加idea8 开发grails的流程

      grails学习遇到的问题

      在grails初学中遇到的一些问题,适合于初学者参考,或许你也遇到了这些问题

      Groovy轻松入门——Grails实战基础篇

      在学习任何东西之前,最重要的是培养兴趣,Groovy世界最耀眼的技术之一--Grails相信大家早已耳闻,我将通过Grails实战系列文章 向您展现Grails的迷人风采,使您感受到Grails的魅力,以至疯狂地爱上Grails,并坠入...

      grails开发环境配置及应用开发

      详细讲解grails开发环境配置。 详细讲解grails连接mysql数据库,crud开发

      Grails1.1中文文档

      Grails1.1中文文档

      grails+Xfire webservice

      grails+Xfire webservice

      grails 1.0.4

      Grails专为下一代JavaWeb应用程序而设计的框架,其借助于Groovy动态语言,使Web开发变得简单而方便。Grails尽量为更多现有的Java项目创建一个全面的框架(不仅局限于视图处理),这和当前一些Java框架提供给用户的一...

      grails

      grails-2.1.zip.001

      Grails中文参考手册

      Grails 中文 参考手册

      grails框架

      grails的插件系统也是其亮点之一。首先,和rails,django等web框架类似,基于微内核的思想,插件(可重用模块)是框架的一等公民。grails除了核心模块以外的功能几乎都是通过插件方式实现的。实际上,一个grails插件...

      grails 中文文档+grails-fckeditor-0.9.5.zip插件

      grails 中文文档+grails-fckeditor-0.9.5.zip插件

      Grails1.3.7参考手册

      Grails 1.3.7英文版官方参考手册,学习Grails的权威指南

      grails入门经典

      grails grails入门经典 grails入门 grails例子 grails资料 通过自学一点点积累起来的,相信对你有帮助的。

      Grails 教程

      Grails是一个full-stack框架,它借助于核心技术与相关的插件(plug-in)来解决Web开发中方方面面的问题,其中包括: 易于使用的基于Hibernate的对象-关系映射(ORM)层 称为Groovy Server Pages (GSP)的表现层技术 ...

    Global site tag (gtag.js) - Google Analytics