Struts框架核心工作流程与原理

来源:互联网 发布:874是什么意思网络用语 编辑:程序博客网 时间:2024/06/05 21:50

Struts框架核心工作流程与原理


<iframe id="cproIframe1" src="http://pos.baidu.com/acom?adn=1&amp;at=99&amp;aurl=&amp;cad=1&amp;ccd=32&amp;cec=UTF-8&amp;cfv=0&amp;ch=0&amp;col=zh-CN&amp;conOP=0&amp;cpa=1&amp;dai=1&amp;dis=0&amp;ltr=http%3A%2F%2Fm.ddvip.com%2Ftech%2F1000143653.html&amp;ltu=http%3A%2F%2Fm.ddvip.com%2Ftech%2F1000143653.html&amp;lunum=6&amp;n=23010034_cpr&amp;pcs=360x570&amp;pis=10000x10000&amp;ps=138x0&amp;psr=720x1280&amp;pss=360x138&amp;qn=c68fad948d68338d&amp;rad=&amp;rsi0=360&amp;rsi1=54&amp;rsi5=4&amp;rss0=&amp;rss1=&amp;rss2=&amp;rss3=&amp;rss4=&amp;rss5=&amp;rss6=&amp;rss7=&amp;scale=20.3&amp;skin=mobile_skin_white_blue&amp;td_id=2184327&amp;tn=template_inlay_all_mobile&amp;tpr=1438406649545&amp;ts=1&amp;xuanting=0&amp;tt=1438406649489.63.702.740&amp;dtm=BAIDU_DUP2_SETJSONADSLOT&amp;dc=2&amp;di=u2184327&amp;ti=Struts%E6%A1%86%E6%9E%B6%E6%A0%B8%E5%BF%83%E5%B7%A5%E4%BD%9C%E6%B5%81%E7%A8%8B%E4%B8%8E%E5%8E%9F%E7%90%86--%E8%B1%86%E8%B1%86%E7%BD%91&amp;wt=1&amp;distp=1001&amp;conW=360&amp;conH=54" width="360" height="54" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true"></iframe>

1、Struts2架构图 
  这是Struts2官方站点提供的Struts 2 的整体结构。 
  

  执行流程图

  

 2、Struts2部分类介绍 
  这部分从Struts2参考文档中翻译就可以了。 
ActionMapper 
       ActionMapper其实是HttpServletRequest和Action调用请求的一个映射,它屏蔽了Action对于Request等 java Servlet类的依赖。Struts2中它的默认实现类是DefaultActionMapper,ActionMapper很大的用处可以根据自己的需要来设计url格式,它自己也有Restful的实现,具体可以参考文档的docsactionmapper.html。 
ActionProxy&ActionInvocation 
       Action的一个代理,由ActionProxyFactory创建,它本身不包括Action实例,默认实现DefaultActionProxy是由ActionInvocation持有Action实例。ActionProxy作用是如何取得Action,无论是本地还是远程。而 ActionInvocation的作用是如何执行Action,拦截器的功能就是在ActionInvocation中实现的。 
ConfigurationProvider&Configuration 
       ConfigurationProvider就是Struts2中配置文件的解析器,Struts2中的配置文件主要是尤其实现类 XmlConfigurationProvider及其子类StrutsXmlConfigurationProvider来解析。 

    3、Struts2请求流程 
  1、客户端发送请求 
  2、请求先通过ActionContextCleanUp-->FilterDispatcher 
  3、FilterDispatcher通过ActionMapper来决定这个Request需要调用哪个Action 
  4、如果ActionMapper决定调用某个Action,FilterDispatcher把请求的处理交给ActionProxy,这儿已经转到它的Delegate--Dispatcher来执行 
  5、ActionProxy根据ActionMapping和ConfigurationManager找到需要调用的Action类 
  6、ActionProxy创建一个ActionInvocation的实例 
  7、ActionInvocation调用真正的Action,当然这涉及到相关拦截器的调用 
  8、Action执行完毕,ActionInvocation创建Result并返回,当然,如果要在返回之前做些什么,可以实现PreResultListener。添加PreResultListener可以在Interceptor中实现。 


  另一个版本:大同小异~ 
  一个请求在Struts2框架中的处理大概分为以下几个步骤: 
  1.客户端提起一个(HttpServletRequest)请求,如上文在浏览器中输入”http://localhost:8080/TestMvc/add.action”就是提起一个(HttpServletRequest)请求。 
  2.请求被提交到一系列(主要是三层)的过滤器(Filter),如(ActionContextCleanUp、其他过滤器(SiteMesh等)、 FilterDispatcher)。注意这里是有顺序的,先ActionContextCleanUp,再其他过滤器(SiteMesh等)、最后到 FilterDispatcher。 
  3.FilterDispatcher是控制器的核心,就是mvc中c控制层的核心。下面粗略的分析下我理解的FilterDispatcher工作流程和原理:FilterDispatcher进行初始化并启用核心doFilter 

其代码如下: 

Java代码



public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException ...{           HttpServletRequest request = (HttpServletRequest) req;           HttpServletResponse response = (HttpServletResponse) res;           ServletContext servletContext = filterConfig.getServletContext();           // 在这里处理了HttpServletRequest和HttpServletResponse。           DispatcherUtils du = DispatcherUtils.getInstance();           du.prepare(request, response);//正如这个方法名字一样进行locale、encoding以及特殊request parameters设置           try ...{               request = du.wrapRequest(request, servletContext);//对request进行包装           } catch (IOException e) ...{               String message = "Could not wrap servlet request with MultipartRequestWrapper!";               LOG.error(message, e);               throw new ServletException(message, e);           }                   ActionMapperIF mapper = ActionMapperFactory.getMapper();//得到action的mapper           ActionMapping mapping = mapper.getMapping(request);// 得到action 的 mapping           if (mapping == null) ...{               // there is no action in this request, should we look for a static resource?               String resourcePath = RequestUtils.getServletPath(request);               if ("".equals(resourcePath) && null != request.getPathInfo()) ...{                   resourcePath = request.getPathInfo();               }               if ("true".equals(Configuration.get(WebWorkConstants.WEBWORK_SERVE_STATIC_CONTENT))                        && resourcePath.startsWith("/webwork")) ...{                   String name = resourcePath.substring("/webwork".length());                   findStaticResource(name, response);               } else ...{                   // this is a normal request, let it pass through                   chain.doFilter(request, response);               }               // WW did its job here               return;           }           Object o = null;           try ...{               //setupContainer(request);               o = beforeActionInvocation(request, servletContext);              //整个框架最最核心的方法,下面分析               du.serviceAction(request, response, servletContext, mapping);           } finally ...{               afterActionInvocation(request, servletContext, o);               ActionContext.setContext(null);           }       }   du.serviceAction(request, response, servletContext, mapping);   //这个方法询问ActionMapper是否需要调用某个Action来处理这个(request)请求,如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给ActionProxy   public void serviceAction(HttpServletRequest request, HttpServletResponse response, String namespace, String actionName, Map requestMap, Map parameterMap, Map sessionMap, Map applicationMap) ...{            HashMap extraContext = createContextMap(requestMap, parameterMap, sessionMap, applicationMap, request, response, getServletConfig());  //实例化Map请求 ,询问ActionMapper是否需要调用某个Action来处理这个(request)请求           extraContext.put(SERVLET_DISPATCHER, this);            OgnlValueStack stack = (OgnlValueStack) request.getAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY);            if (stack != null) ...{                extraContext.put(ActionContext.VALUE_STACK,new OgnlValueStack(stack));            }            try ...{                ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext);    //这里actionName是通过两道getActionName解析出来的, FilterDispatcher把请求的处理交给ActionProxy,下面是ServletDispatcher的 TODO:                request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack());                proxy.execute();               //通过代理模式执行ActionProxy               if (stack != null)...{                    request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY,stack);                }            } catch (ConfigurationException e) ...{                log.error("Could not find action", e);                sendError(request, response, HttpServletResponse.SC_NOT_FOUND, e);            } catch (Exception e) ...{                log.error("Could not execute action", e);                sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);            }    }

4.FilterDispatcher询问ActionMapper是否需要调用某个Action来处理这个(request)请求,如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给ActionProxy。 
  5.ActionProxy通过Configuration Manager(struts.xml)询问框架的配置文件,找到需要调用的Action类. 
如上文的struts.xml配置

0 0
原创粉丝点击