这是Struts2官方站点提供的Struts 2 的整体结构。
一个请求在Struts2框架中的处理大概分为以下几个步骤:
- 客户端提起一个(HttpServletRequest)请求,如上文在浏览器中输入”http://localhost:8080/TestMvc/add.action”就是提起一个(HttpServletRequest)请求。
- 请求被提交到一系列(主要是三层)的过滤器(Filter),如(ActionContextCleanUp、其他过滤器(SiteMesh等)、 FilterDispatcher)。注意这里是有顺序的,先ActionContextCleanUp,再其他过滤器(SiteMesh等)、最后到FilterDispatcher。
- FilterDispatcher是控制器的核心,就是mvc中c控制层的核心。下面粗略的分析下我理解的FilterDispatcher工作流程和原理:FilterDispatcher进行初始化并启用核心doFilter
其代码如下:FilterDispatcher询问ActionMapper是否需要调用某个Action来处理这个(request)请求,如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给ActionProxy。- 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);
- }
- }
- ActionProxy通过Configuration Manager(struts.xml)询问框架的配置文件,找到需要调用的Action类.
如上文的struts.xml配置如果提交请求的是add.action,那么找到的Action类就是edisundong.AddAction。- <?xml version="1.0" encoding="GBK"?>
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <include file="struts-default.xml"/>
- <package name="struts2" extends="struts-default">
- <action name="add"
- class="edisundong.AddAction" >
- <result>add.jsp</result>
- </action>
- </package>
- </struts>
- 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文件摘取的内容:一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果。如上文中将结构返回“add.jsp”,但大部分时候都是返回另外一个action,那么流程又得走一遍………- < interceptor name ="alias" class ="com.opensymphony.xwork2.interceptor.AliasInterceptor" />
- < interceptor name ="autowiring" class ="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor" />
- < interceptor name ="chain" class ="com.opensymphony.xwork2.interceptor.ChainingInterceptor" />
- < interceptor name ="conversionError" class ="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor" />
- < interceptor name ="createSession" class ="org.apache.struts2.interceptor.CreateSessionInterceptor" />
- < interceptor name ="debugging" class ="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />
- < interceptor name ="external-ref" class ="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor" />
- < interceptor name ="execAndWait" class ="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor" />
- < interceptor name ="exception" class ="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor" />
- < interceptor name ="fileUpload" class ="org.apache.struts2.interceptor.FileUploadInterceptor" />
- < interceptor name ="i18n" class ="com.opensymphony.xwork2.interceptor.I18nInterceptor" />
- < interceptor name ="logger" class ="com.opensymphony.xwork2.interceptor.LoggingInterceptor" />
- < interceptor name ="model-driven" class ="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor" />
- < interceptor name ="scoped-model-driven" class ="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor" />
- < interceptor name ="params" class ="com.opensymphony.xwork2.interceptor.ParametersInterceptor" />
- < interceptor name ="prepare" class ="com.opensymphony.xwork2.interceptor.PrepareInterceptor" />
- < interceptor name ="static-params" class ="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor" />
- < interceptor name ="scope" class ="org.apache.struts2.interceptor.ScopeInterceptor" />
- < interceptor name ="servlet-config" class ="org.apache.struts2.interceptor.ServletConfigInterceptor" />
- < interceptor name ="sessionAutowiring" class ="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor" />
- < interceptor name ="timer" class ="com.opensymphony.xwork2.interceptor.TimerInterceptor" />
- < interceptor name ="token" class ="org.apache.struts2.interceptor.TokenInterceptor" />
- < interceptor name ="token-session" class ="org.apache.struts2.interceptor.TokenSessionStoreInterceptor" />
- < interceptor name ="validation" class ="com.opensymphony.xwork2.validator.ValidationInterceptor" />
- < interceptor name ="workflow" class ="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor" />
- < interceptor name ="store" class ="org.apache.struts2.interceptor.MessageStoreInterceptor" />
- < interceptor name ="checkbox" class ="org.apache.struts2.interceptor.CheckboxInterceptor" />
- < interceptor name ="profiling" class ="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />
总结:
Struts2的工作流就只有这7步,比起Struts1简单了很多(本人能力有限,struts2更多的东西现在还看不明白)。网上有很多很多的关于.net和java的比较之类的文章,可是有几个作者是真正用过java和.net的呢?更多的评论都是人云亦云,想当然的评论java和.net。作为技术人千万不要屁股决定脑袋,关于web的设计模式上.net也不是那么一无是处,java也不是那么完美无缺。下一篇分析下ASP.NET的设计模式(生命周期)。
相关推荐
struts2核心工作流程与原理.doc
下面将详细讲解Struts2的核心工作流程与原理。 1. **请求发起**: 当用户在浏览器中输入URL,如`http://localhost:8080/TestMvc/add.action`,这构成了一个HTTP请求(HttpServletRequest)。这个请求会被Web服务器...
本专题资料详细介绍了Struts2的核心工作流程与原理,以下是对该流程的深入解析: 1. **客户端请求**: 当用户在浏览器中输入URL(例如`http://localhost:8080/TestMvc/add.action`)时,发起一个HTTP请求。这个...
其中,ActionContextCleanUp过滤器是可选的,但它在Struts2与其他框架(如SiteMeshPlugin)的集成中扮演了重要角色,确保了环境的干净与隔离,防止不同请求之间的数据污染。 #### 3. FilterDispatcher调用 接下来...
要深入学习和掌握Struts2,建议阅读官方文档,参与实际项目实践,也可以参考相关的技术书籍和教程,例如《Struts2技术内幕——深入解析Struts2架构设计与实现原理》等资源,来提升对Struts2框架的全面理解。
总之,《Struts2技术内幕——深入解析Struts2架构设计与实现原理》配合《struts2基础.chm》,将帮助读者全面掌握Struts2的架构设计、核心组件、配置方式、插件使用以及源码解读,对于想要在Java Web领域深入发展的...
### Struts1与Struts2原理及区别详解 #### Struts1原理概述 **Struts1** 是一种基于MVC架构的开源Java Web框架,它主要用于构建动态网站和应用程序。Struts1的核心组件包括ActionServlet、ActionForm以及Action...
**Struts2与Spring MVC比较:** 1. **灵活性**:Spring MVC允许更多的自定义,如自定义拦截器、视图解析器,而Struts2的扩展性相对弱些。 2. **依赖注入**:Spring MVC是Spring框架的一部分,天然支持DI,而Struts2...
《Struts2技术内幕-深入解析Struts2架构设计与实现原理》这本书深入探讨了Struts2的核心概念、架构和实现机制。 1. **Struts2架构设计**:Struts2的架构基于拦截器(Interceptor)模式,它将业务逻辑和表现层解耦,...
Struts 体系结构与工作原理 Struts 体系结构是目前基于 Java 的 Web 系统设计中广泛使用的 MVC ...Struts 体系结构与工作原理是 Java Web 开发中的一种常见的设计模式,能够帮助开发者快速构建高质量的 Web 应用程序。
#### 二、Struts2与WebWork的关系 - **Struts2**是由Struts社区和WebWork社区共同研发的产物。 - **WebWork**是Struts2的核心技术支撑,Struts2在其基础上进行了增强和改进。 - **Struts2**继承了WebWork的设计理念...
"Struts2核心jar包"是实现这一框架的基础,它包含了运行Struts2应用程序所必需的类库。 Struts2的核心jar包主要包括以下几个部分: 1. **Action**:这是Struts2的核心组件,负责处理用户请求。Action接口定义了...
本文将深入探讨Struts2的入门实例、工作原理及其主要组件。 首先,让我们从一个简单的Struts2入门实例开始。创建一个基本的Struts2应用通常包括以下几个步骤: 1. 引入Struts2的依赖库到项目中,这通常通过Maven或...
**Struts2核心工作流程与原理** 1. **流程概述**:在Struts2中,请求首先由`StrutsPrepareAndExecuteFilter`处理。如果请求是Action,框架将创建`ActionInvocation`,并通过`ActionProxy`准备`Action`和拦截器链。...
下面我们将深入探讨Struts2的核心概念、工作原理以及它如何帮助开发者构建高效、可维护的Web应用。 1. **Struts2框架概述** Struts2是Apache软件基金会下的一个项目,它继承了最初的Struts1框架并进行了许多改进,...
9. **开发文档**:包含在压缩包中的开发文档是理解Struts2框架的重要资源,它详细介绍了每个组件的工作原理、配置选项以及最佳实践。 通过学习和使用Struts2的核心包,开发者可以深入理解MVC架构,掌握Java Web开发...
理解Struts2的工作原理是深入学习和有效利用该框架的关键。下面将详细介绍Struts2的工作流程。 1. **请求接收**:当用户在浏览器中提交一个HTTP请求时,这个请求首先会被Web服务器(如Apache Tomcat)接收到。如果...
Struts2是一个强大的Java web应用程序框架,用于...通过分析源代码,可以深入理解Struts2的工作原理、配置机制以及MVC模式在实际项目中的应用。对于初学者,这是一个很好的实践平台,能帮助他们掌握Web开发的基本技能。