设为首页收藏本站

Scripts 学盟

 找回密码
 加入学盟

QQ登录

只需一步,快速开始

查看: 3421|回复: 3
打印 上一主题 下一主题

struts2核心工作流程与原理1 [复制链接]

Rank: 8Rank: 8

跳转到指定楼层
1#
那个谁 发表于 2011-6-7 17:37:04 |只看该作者 |倒序浏览
本帖最后由 那个谁 于 2011-6-7 17:37 编辑

o_File.png
这是Struts2官方站点提供的Struts 2 的整体结构。
一个请求在Struts2框架中的处理大概分为以下几个步骤
1.     客户端提起一个(HttpServletRequest)请求,如上文在浏览器中输入”http://localhost:8080/TestMvc/add.action”就是提起一个(HttpServletRequest)请求。
2.     请 求被提交到一系列(主要是三层)的过滤器(Filter),如(ActionContextCleanUp、其他过滤器(SiteMesh等)、 FilterDispatcher)。注意这里是有顺序的,先ActionContextCleanUp,再其他过滤器(SiteMesh等)、最后到 FilterDispatcher。
3.     FilterDispatcher是控制器的核心,就是mvc中c控制层的核心。下面粗略的分析下我理解的FilterDispatcher工作流程和原理:
  1. FilterDispatcher进行初始化并启用核心doFilter
  2. 其代码如下:
  3. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
  4.         HttpServletRequest request = (HttpServletRequest) req;
  5.         HttpServletResponse response = (HttpServletResponse) res;
  6.         ServletContext servletContext = filterConfig.getServletContext();
  7.         // 在这里处理了HttpServletRequest和HttpServletResponse。
  8.         DispatcherUtils du = DispatcherUtils.getInstance();
  9.         du.prepare(request, response);//正如这个方法名字一样进行locale、encoding以及特殊request parameters设置
  10.         try {
  11.             request = du.wrapRequest(request, servletContext);//对request进行包装
  12.         } catch (IOException e) {
  13.             String message = "Could not wrap servlet request with MultipartRequestWrapper!";
  14.             LOG.error(message, e);
  15.             throw new ServletException(message, e);
  16.         }
  17.                 ActionMapperIF mapper = ActionMapperFactory.getMapper();//得到action的mapper
  18.         ActionMapping mapping = mapper.getMapping(request);// 得到action 的 mapping
  19.         if (mapping == null) {
  20.             // there is no action in this request, should we look for a static resource?
  21.             String resourcePath = RequestUtils.getServletPath(request);
  22.             if ("".equals(resourcePath) && null != request.getPathInfo()) {
  23.                 resourcePath = request.getPathInfo();
  24.             }
  25.             if ("true".equals(Configuration.get(WebWorkConstants.WEBWORK_SERVE_STATIC_CONTENT))
  26.                     && resourcePath.startsWith("/webwork")) {
  27.                 String name = resourcePath.substring("/webwork".length());
  28.                 findStaticResource(name, response);
  29.             } else {
  30.                 // this is a normal request, let it pass through
  31.                 chain.doFilter(request, response);
  32.             }
  33.             // WW did its job here
  34.             return;
  35.         }
  36.         Object o = null;
  37.         try {
  38.             //setupContainer(request);
  39.             o = beforeActionInvocation(request, servletContext);
  40. //整个框架最最核心的方法,下面分析
  41.             du.serviceAction(request, response, servletContext, mapping);

  42.         } finally {
  43.             afterActionInvocation(request, servletContext, o);
  44.             ActionContext.setContext(null);
  45.         }
  46.     }

  47. du.serviceAction(request, response, servletContext, mapping);
  48. //这个方法询问ActionMapper是否需要调用某个Action来处理这个(request)请求,如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给ActionProxy

  49. public void serviceAction(HttpServletRequest request, HttpServletResponse response, String namespace, String actionName, Map requestMap, Map parameterMap, Map sessionMap, Map applicationMap) {
  50.         HashMap extraContext = createContextMap(requestMap, parameterMap, sessionMap, applicationMap, request, response, getServletConfig());  //实例化Map请求 ,询问ActionMapper是否需要调用某个Action来处理这个(request)请求
  51.         extraContext.put(SERVLET_DISPATCHER, this);
  52.         OgnlValueStack stack = (OgnlValueStack) request.getAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY);
  53.         if (stack != null) {
  54.             extraContext.put(ActionContext.VALUE_STACK,new OgnlValueStack(stack));
  55.         }
  56.         try {
  57.             ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext);
  58. //这里actionName是通过两道getActionName解析出来的, FilterDispatcher把请求的处理交给ActionProxy,下面是ServletDispatcher的 TODO:
  59.             request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack());
  60.             proxy.execute();
  61.          //通过代理模式执行ActionProxy
  62.             if (stack != null){
  63.                 request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY,stack);
  64.             }
  65.         } catch (ConfigurationException e) {
  66.             log.error("Could not find action", e);
  67.             sendError(request, response, HttpServletResponse.SC_NOT_FOUND, e);
  68.         } catch (Exception e) {
  69.             log.error("Could not execute action", e);
  70.             sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
  71.         }
  72. }
复制代码
4.     FilterDispatcher询问ActionMapper是否需要调用某个Action来处理这个(request)请求,如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给ActionProxy。
5.     ActionProxy通过Configuration Manager(struts.xml)询问框架的配置文件,找到需要调用的Action类.

     如上文的struts.xml配置
  1. <?xml version="1.0" encoding="GBK"?>
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
  3. <struts>
  4.      <include file="struts-default.xml"/>
  5.      <package name="struts2" extends="struts-default">
  6.          <action name="add"
  7.              class="edisundong.AddAction" >
  8.              <result>add.jsp</result>
  9.          </action>   
  10.      </package>
  11. </struts>
复制代码
如果提交请求的是add.action,那么找到的Action类就是edisundong.AddAction。

6.     ActionProxy创建一个ActionInvocation的实例,同时ActionInvocation通过代理模式调用Action。但在调用之前ActionInvocation会根据配置加载Action相关的所有Interceptor。(Interceptor是struts2另一个核心级的概念)
[code]下面我们来看看ActionInvocation是如何工作的:
ActionInvocation 是Xworks 中Action 调度的核心。而对Interceptor 的调度,也正是由ActionInvocation负责。
ActionInvocation 是一个接口, 而DefaultActionInvocation 则是Webwork 对ActionInvocation的默认实现。
1

查看全部评分

分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏0

Rank: 9Rank: 9Rank: 9

2#
莺子 发表于 2011-6-8 08:11:53 |只看该作者
很。。。。。。。。。。。。。。。。。

使用道具 举报

Rank: 8Rank: 8

3#
那个谁 发表于 2011-6-9 12:12:11 |只看该作者
莺子 发表于 2011-6-8 08:11
很。。。。。。。。。。。。。。。。。

为啥。。。。。。。。。。。。。。

使用道具 举报

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

4#
混混@普宁.中国 实名认证  发表于 2011-8-19 16:19:39 |只看该作者
是很没看懂吧

使用道具 举报

您需要登录后才可以回帖 登录 | 加入学盟

手机版|Scripts 学盟   |

GMT+8, 2024-5-9 23:07 , Processed in 1.067206 second(s), 15 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部