当前位置:编程学习 > JAVA >>

Struts2体系结构图以及详解

 Strut2的体系结构如图所示:

一个请求在Struts2框架中的处理大概分为以下几个步骤:
 

1、客户端初始化一个指向Servlet容器(例如Tomcat)的请求;
2、这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助,例如:SiteMesh Plugin);
3、接着FilterDispatcher被调用,FilterDispatcher询问ActionMapper来决定这个请求是否需要调用某个Action;
4、如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给ActionProxy;
5、ActionProxy通过Configuration Manager询问框架的配置文件,找到需要调用的Action类;
6、ActionProxy创建一个ActionInvocation的实例。
7、ActionInvocation实例使用命名模式来调用,在调用Action的过程前后,涉及到相关拦截器(Intercepter)的调用。
8、一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果。返回结果通常是(但不总是,也可能是另外的一个Action链)一个需要被表示的JSP或者FreeMarker的模版。在表示的过程中可以使用Struts2框架中继承的标签。在这个过程中需要涉及到ActionMapper。


FilterDispatcher是控制器的核心,就是mvc中c控制层的核心。下面粗略的分析下我理解的FilterDispatcher工作流程和原理:FilterDispatcher进行初始化并启用核心doFilter
其代码如下:
[java]
1. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException ...{ 
2.         HttpServletRequest request = (HttpServletRequest) req; 
3.         HttpServletResponse response = (HttpServletResponse) res; 
4.         ServletContext servletContext = filterConfig.getServletContext(); 
5.         // 在这里处理了HttpServletRequest和HttpServletResponse。  
6.         DispatcherUtils du = DispatcherUtils.getInstance(); 
7.         du.prepare(request, response);//正如这个方法名字一样进行locale、encoding以及特殊request parameters设置  
8.         try ...{ 
9.             request = du.wrapRequest(request, servletContext);//对request进行包装  
10.         } catch (IOException e) ...{ 
11.             String message = "Could not wrap servlet request with MultipartRequestWrapper!"; 
12.             LOG.error(message, e); 
13.             throw new ServletException(message, e); 
14.         } 
15.                 ActionMapperIF mapper = ActionMapperFactory.getMapper();//得到action的mapper  
16.         ActionMapping mapping = mapper.getMapping(request);// 得到action 的 mapping  
17.         if (mapping == null) ...{ 
18.             // there is no action in this request, should we look for a static resource?  
19.             String resourcePath = RequestUtils.getServletPath(request); 
20.             if ("".equals(resourcePath) && null != request.getPathInfo()) ...{ 
21.                 resourcePath = request.getPathInfo(); 
22.             } 
23.             if ("true".equals(Configuration.get(WebWorkConstants.WEBWORK_SERVE_STATIC_CONTENT))  
24.                     && resourcePath.startsWith("/webwork")) ...{ 
25.                 String name = resourcePath.substring("/webwork".length()); 
26.                 findStaticResource(name, response); 
27.             } else ...{ 
28.                 // this is a normal request, let it pass through  
29.                 chain.doFilter(request, response); 
30.             } 
31.             // WW did its job here  
32.             return; 
33.         } 
34.         Object o = null; 
35.         try ...{ 
36.             //setupContainer(request);  
37.             o = beforeActionInvocation(request, servletContext); 
38. //整个框架最最核心的方法,下面分析  
39.             du.serviceAction(request, response, servletContext, mapping); 
40.         } finally ...{ 
41.             afterActionInvocation(request, servletContext, o); 
42.             ActionContext.setContext(null); 
43.         } 
44.     } 
45. du.serviceAction(request, response, servletContext, mapping); 
46. //这个方法询问ActionMapper是否需要调用某个Action来处理这个(request)请求,如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给ActionProxy  
47. public void serviceAction(HttpServletRequest request, HttpServletResponse response, String namespace, String actionName, Map requestMap, Map parameterMap, Map sessionMap, Map applicationMap) ...{  
48.         HashMap extraContext = createContextMap(requestMap, parameterMap, sessionMap, applicationMap, request, response, getServletConfig());  //实例化Map请求 ,询问ActionMapper是否需要调用某个Action来处理这个(request)请求  www.zzzyk.com
49.         extraContext.put(SERVLET_DISPATCHER, this);  
50.         OgnlValueStack stack = (OgnlValueStack) request.getAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY);  
51.         if (stack != null) ...{  
补充:软件开发 , Java ,

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,