struts2核心工作流程与原理_struts2工作流程原理

2020-02-28 其他范文 下载本文

struts2核心工作流程与原理由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“struts2工作流程原理”。

struts2核心工作流程与原理 收藏

这是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工作流程和原理:FilterDispatcher进行初始化并启用核心doFilter

其代码如下:

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 meage = “Could not wrap servlet request with MultipartRequestWrapper!”;12.LOG.error(meage, e);

13.throw new ServletException(meage, 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 pa 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.48.public void serviceAction(HttpServletRequest request, HttpServletResponse response, String namespace, String actionName, Map requestMap, Map parameterMap, Map seionMap, Map applicationMap)...{

49.HashMap extraContext = createContextMap(requestMap, parameterMap, seionMap, applicationMap, request, response, getServletConfig());//实例化Map请求,询问ActionMapper是否需要调用某个Action来处理这个(request)请求

50.extraContext.put(SERVLET_DISPATCHER, this);51.OgnlValueStack stack =(OgnlValueStack)request.getAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY);

52.if(stack!= null)...{

53.extraContext.put(ActionContext.VALUE_STACK,new OgnlValueStack(stack));54.} 55.try...{

56.ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext);

57.//这里actionName是通过两道getActionName解析出来的, FilterDispatcher把请求的处理交给ActionProxy,下面是ServletDispatcher的 TODO:

58.request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack());59.proxy.execute();

60.//通过代理模式执行ActionProxy 61.if(stack!= null)...{

62.request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY,stack);63.}

64.} catch(ConfigurationException e)...{ 65.log.error(“Could not find action”, e);

66.sendError(request, response, HttpServletResponse.SC_NOT_FOUND, e);

67.} catch(Exception e)...{ 68.log.error(“Could not execute action”, e);

69.sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);70.} 71.} 72.FilterDispatcher询问ActionMapper是否需要调用某个Action来处理这个(request)请求,如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给ActionProxy。

4.ActionProxy通过Configuration Manager(struts.xml)询问框架的配置文件,找到需要调用的Action类.如上文的struts.xml配置

1.

2.3.4.5.

6.

7.cla=“edisundong.AddAction” > 8.add.jsp 9.10. 11.12.如果提交请求的是add.action,那么找到的Action类就是edisundong.AddAction。

5.ActionProxy创建一个ActionInvocation的实例,同时ActionInvocation通过代理模式调用Action。但在调用之前ActionInvocation会根据配置加载Action相关的所有Interceptor。(Interceptor是struts2另一个核心级的概念)

下面我们来看看ActionInvocation是如何工作的:

ActionInvocation 是Xworks 中Action 调度的核心。而对Interceptor 的调度,也正是由ActionInvocation负责。ActionInvocation 是一个接口,而DefaultActionInvocation 则是Webwork 对ActionInvocation的默认实现。

Interceptor 的调度流程大致如下:

1.ActionInvocation初始化时,根据配置,加载Action相关的所有Interceptor。

2.通过ActionInvocation.invoke方法调用Action实现时,执行Interceptor。

Interceptor将很多功能从我们的Action中独立出来,大量减少了我们Action的代码,独立出来的行为具有很好的重用性。XWork、WebWork的许多功能都是有Interceptor实现,可以在配置文件中组装Action用到的Interceptor,它会按照你指定的顺序,在Action执行前后运行。那么什么是拦截器。

拦截器就是AOP(Aspect-Oriented Programming)的一种实现。(AOP是指用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。)

拦截器的例子这里就不展开了。struts-default.xml文件摘取的内容:

1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果。如上文中将结构返回“add.jsp”,但大部分时候都是返回另外一个action,那么流程又得走一遍………

《struts2核心工作流程与原理.docx》
将本文的Word文档下载,方便收藏和打印
推荐度:
struts2核心工作流程与原理
点击下载文档
相关专题 struts2工作流程原理 工作流程 原理 核心 struts2工作流程原理 工作流程 原理 核心
[其他范文]相关推荐
    [其他范文]热门文章
      下载全文