struts2核心工作流程与原理

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

 

2. Struts2部分类介绍 
这部分从Struts2参考文档中翻译就可以了。 
ActionMapper 
        ActionMapper其实是HttpServletRequest和Action调用请求的一个映射,它屏蔽了Action对于Request等 java Servlet类的依赖。Struts2中它的默认实现类是DefaultActionMapper,ActionMapper很大的用处可以根据自己的需要来设计url格式,它自己也有Restful的实现,具体可以参考文档的docs\actionmapper.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代码  
  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 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 normal request, let it pass through   
  29.                 chain.doFilter(request, response);   
  30.               
  31.             // WW did its job here   
  32.             return  
  33.           
  34.         Object null  
  35.         try ...{   
  36.             //setupContainer(request);   
  37.             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)请求   
  49.         extraContext.put(SERVLET_DISPATCHER, this);    
  50.         OgnlValueStack stack (OgnlValueStack) request.getAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY);    
  51.         if (stack != null...{    
  52.             extraContext.put(ActionContext.VALUE_STACK,new OgnlValueStack(stack));    
  53.            
  54.         try ...{    
  55.             ActionProxy proxy ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext);    
  56. //这里actionName是通过两道getActionName解析出来的, FilterDispatcher把请求的处理交给ActionProxy,下面是ServletDispatcher的 TODO:    
  57.             request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack());    
  58.             proxy.execute();    
  59.            //通过代理模式执行ActionProxy   
  60.             if (stack != null)...{    
  61.                 request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY,stack);    
  62.                
  63.         catch (ConfigurationException e) ...{    
  64.             log.error("Could not find action"e);    
  65.             sendError(request, response, HttpServletResponse.SC_NOT_FOUND, e);    
  66.         catch (Exception e) ...{    
  67.             log.error("Could not execute action"e);    
  68.             sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);    
  69.            
  70.    




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

Java代码  
  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另一个核心级的概念) 

下面我们来看看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文件摘取的内容: 

Java代码  
  1. interceptor name ="alias" class ="com.opensymphony.xwork2.interceptor.AliasInterceptor" />    
  2. interceptor name ="autowiring" class ="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor" />    
  3. interceptor name ="chain" class ="com.opensymphony.xwork2.interceptor.ChainingInterceptor" />    
  4. interceptor name ="conversionError" class ="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor" />    
  5. interceptor name ="createSession" class ="org.apache.struts2.interceptor.CreateSessionInterceptor" />    
  6. interceptor name ="debugging" class ="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />    
  7. interceptor name ="external-ref" class ="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor" />    
  8. interceptor name ="execAndWait" class ="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor" />    
  9. interceptor name ="exception" class ="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor" />    
  10. interceptor name ="fileUpload" class ="org.apache.struts2.interceptor.FileUploadInterceptor" />    
  11. interceptor name ="i18n" class ="com.opensymphony.xwork2.interceptor.I18nInterceptor" />    
  12. interceptor name ="logger" class ="com.opensymphony.xwork2.interceptor.LoggingInterceptor" />    
  13. interceptor name ="model-driven" class ="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor" />    
  14. interceptor name ="scoped-model-driven" class ="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor" />    
  15. interceptor name ="params" class ="com.opensymphony.xwork2.interceptor.ParametersInterceptor" />    
  16. interceptor name ="prepare" class ="com.opensymphony.xwork2.interceptor.PrepareInterceptor" />    
  17. interceptor name ="static-params" class ="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor" />    
  18. interceptor name ="scope" class ="org.apache.struts2.interceptor.ScopeInterceptor" />    
  19. interceptor name ="servlet-config" class ="org.apache.struts2.interceptor.ServletConfigInterceptor" />    
  20. interceptor name ="sessionAutowiring" class ="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor" />    
  21. interceptor name ="timer" class ="com.opensymphony.xwork2.interceptor.TimerInterceptor" />    
  22. interceptor name ="token" class ="org.apache.struts2.interceptor.TokenInterceptor" />    
  23. interceptor name ="token-session" class ="org.apache.struts2.interceptor.TokenSessionStoreInterceptor" />    
  24. interceptor name ="validation" class ="com.opensymphony.xwork2.validator.ValidationInterceptor" />    
  25. interceptor name ="workflow" class ="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor" />    
  26. interceptor name ="store" class ="org.apache.struts2.interceptor.MessageStoreInterceptor" />    
  27. interceptor name ="checkbox" class ="org.apache.struts2.interceptor.CheckboxInterceptor" />    
  28. interceptor name ="profiling" class ="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />   




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

内容概要:本文详细介绍了一种基于Simulink的表贴式永磁同步电机(SPMSM)有限控制集模型预测电流控制(FCS-MPCC)仿真系统。通过构建PMSM数学模型、坐标变换、MPC控制器、SVPWM调制等模块,实现了对电机定子电流的高精度跟踪控制,具备快速动态响应和低稳态误差的特点。文中提供了完整的仿真建模步骤、关键参数设置、核心MATLAB函数代码及仿真结果分析,涵盖转速、电流、转矩和三相电流波形,验证了MPC控制策略在动态性能、稳态精度和抗负载扰动方面的优越性,并提出了参数自整定、加权代价函数、模型预测转矩控制和弱磁扩速等优化方向。; 适合人群:自动化、电气工程及其相关专业本科生、研究生,以及从事电机控制算法研究仿真的工程技术人员;具备一定的电机原理、自动控制理论和Simulink仿真基础者更佳; 使用场景及目标:①用于永磁同步电机模型预测控制的教学演示、课程设计或毕业设计项目;②作为电机先进控制算法(如MPC、MPTC)的仿真验证平台;③支撑科研中对控制性能优化(如动态响应、抗干扰能力)的研究需求; 阅读建议:建议读者结合Simulink环境动手搭建模型,深入理解各模块间的信号流向控制逻辑,重点掌握预测模型构建、代价函数设计开关状态选择机制,并可通过修改电机参数或控制策略进行拓展实验,以增强实践创新能力。
根据原作 https://pan.quark.cn/s/23d6270309e5 的源码改编 湖北省黄石市2021年中考数学试卷所包含的知识点广泛涉及了中学数学的基础领域,涵盖了实数、科学记数法、分式方程、几何体的三视图、立体几何、概率统计以及代数方程等多个方面。 接下来将对每道试题所关联的知识点进行深入剖析:1. 实数倒数的定义:该题目旨在检验学生对倒数概念的掌握程度,即一个数a的倒数表达为1/a,因此-7的倒数可表示为-1/7。 2. 科学记数法的运用:科学记数法是一种表示极大或极小数字的方法,其形式为a×10^n,其中1≤|a|<10,n为整数。 此题要求学生运用科学记数法表示一个天文单位的距离,将1.4960亿千米转换为1.4960×10^8千米。 3. 分式方程的求解方法:考察学生解决包含分母的方程的能力,题目要求找出满足方程3/(2x-1)=1的x值,需通过消除分母的方式转化为整式方程进行解答。 4. 三视图的辨认:该题目测试学生对于几何体三视图(主视图、左视图、俯视图)的认识,需要识别出具有两个相同视图而另一个不同的几何体。 5. 立体几何表面积的计算:题目要求学生计算由直角三角形旋转形成的圆锥的表面积,要求学生对圆锥的底面积和侧面积公式有所了解并加以运用。 6. 统计学的基础概念:题目涉及众数、平均数、极差和中位数的定义,要求学生根据提供的数据信息选择恰当的统计量。 7. 方程的整数解求解:考察学生在实际问题中进行数学建模的能力,通过建立方程来计算在特定条件下帐篷的搭建方案数量。 8. 三角学的实际应用:题目通过在直角三角形中运用三角函数来求解特定线段的长度。 利用正弦定理求解AD的长度是解答该问题的关键。 9. 几何变换的应用:题目要求学生运用三角板的旋转来求解特定点的...
Python基于改进粒子群IPSOLSTM的短期电力负荷预测研究内容概要:本文围绕“Python基于改进粒子群IPSOLSTM的短期电力负荷预测研究”展开,提出了一种结合改进粒子群优化算法(IPSO)长短期记忆网络(LSTM)的混合预测模型。通过IPSO算法优化LSTM网络的关键参数(如学习率、隐层节点数等),有效提升了模型在短期电力负荷预测中的精度收敛速度。文中详细阐述了IPSO算法的改进策略(如引入自适应惯性权重、变异机制等),增强了全局搜索能力避免早熟收敛,并利用实际电力负荷数据进行实验验证,结果表明该IPSO-LSTM模型相较于传统LSTM、PSO-LSTM等方法在预测准确性(如MAE、RMSE指标)方面表现更优。研究为电力系统调度、能源管理提供了高精度的负荷预测技术支持。; 适合人群:具备一定Python编程基础、熟悉基本机器学习算法的高校研究生、科研人员及电力系统相关领域的技术人员,尤其适合从事负荷预测、智能优化算法应用研究的专业人士。; 使用场景及目标:①应用于短期电力负荷预测,提升电网调度的精确性稳定性;②为优化算法(如粒子群算法)深度学习模型(如LSTM)的融合应用提供实践案例;③可用于学术研究、毕业论文复现或电力企业智能化改造的技术参考。; 阅读建议:建议读者结合文中提到的IPSOLSTM原理进行理论学习,重点关注参数优化机制的设计思路,并动手复现实验部分,通过对比不同模型的预测结果加深理解。同时可拓展尝试将该方法应用于其他时序预测场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值