Struts2系统结构及运行原理(1)

2024-05-24 02:58

本文主要是介绍Struts2系统结构及运行原理(1),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、架构图

下边是一张Struts2的官方文档中的Struts2的构架图
这里写图片描述

二、各模块的简要分析

(1)橙色是Servlet Filters,过滤连,所有的请求都要经过Filter的处理;
(2)浅蓝色是Struts Core,是Struts的核心部分,Struts2中已经做好的功能,在实际的开发中不需要动他们;
(3)浅绿色是Interceptor,Struts2的拦截器。Struts2提供了很多默认的拦截器,时刻完成日常开发中的绝大部分工作,当然,也可以自定义拦截器,来实现具体的功能;
(4)浅黄色的是User Creates,即是开发人员完成的工作,包括struts.xml、Action、Template等由开发人员确定。

三、各模块详细说明

(1)FilterDispatcher

FilterDispatcher的四个主要功能:

Master filter for Struts that handles four distinct responsibilities:
1.Executing actions 执行action
2.Cleaning up the ActionContext 清理ActionContext
3.Serving static content 为静态的content提供服务
4.Kicking off XWork's interceptor chain for the request lifecycle 逆转request生命周期中的拦截器过滤链
1.)Executing actions 执行action
This filter executes actions by consulting(咨询) the ActionMapper and determining(决定) if the requested URL should
invoke an action. If the mapper indicates it should, the rest of the filter chain is stopped and the action is invoked. This is important, as it means that filters like the SiteMesh filter must be placed before this filter or they will not be able to decorate the output of actions.

是整个Struts2的调度中心,根据ActionMapper的结果来决定是否处理请求,如果ActionMapper指出该URL应该被Struts2处理,那么它将会执行Action处理,并停止过滤器链上还没有执行的过滤器。上边提到如果有SiteMesh这类的过滤器的话应该放在FilterDispatcher之前(放到FilterDispatcher大家应该知道,它是不会被执行的)。

2.)Cleaning up the link ActionContext
This filter will also automatically(自动) clean up the ActionContext(The ActionContext is the context in which an Action is executed) for you, ensuring that no memory leaks(泄漏)
take place(确保不会有内存泄露发生). However, this can sometimes cause problems integrating with(使一体化) other products like SiteMesh.

下边是FilterDispatcher的一段代码:主要是执行filter

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) req;HttpServletResponse response = (HttpServletResponse) res;ServletContext servletContext = getServletContext();String timerKey = "FilterDispatcher_doFilter: ";try {// FIXME: this should be refactored better to not duplicate work with the action invocationValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();ActionContext ctx = new ActionContext(stack.getContext());ActionContext.setContext(ctx);UtilTimerStack.push(timerKey);request = prepareDispatcherAndWrapRequest(request, response);ActionMapping mapping;try {mapping = actionMapper.getMapping(request, dispatcher.getConfigurationManager());} catch (Exception ex) {log.error("error getting ActionMapping", ex);dispatcher.sendError(request, response, servletContext, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex);return;}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 (staticResourceLoader.canHandle(resourcePath)) {staticResourceLoader.findStaticResource(resourcePath, request, response);} else {// this is a normal request, let it pass throughchain.doFilter(request, response);}// The framework did its job herereturn;}dispatcher.serviceAction(request, response, servletContext, mapping);} finally {dispatcher.cleanUpRequest(request);try {ActionContextCleanUp.cleanUp(req);} finally {UtilTimerStack.pop(timerKey);}devModeOverride.remove();}}

可以看到有这一句:

 mapping = actionMapper.getMapping(request, dispatcher.getConfigurationManager());

我们可以看到这里边的一个映射关系,是通过ActionMapper获取的,下边会说到ActionMapper,getMapping需要的两个参数,一个是request另一个就是我们通过struts.xml配置的信息,下边都会有说。

if (mapping == null) {}

这个判断语句可以看出来如果mapping为空,就是根据判断之后的mapping是一个空值,意思就是这个请求是一个普通的请求,然后下边的操作就是为其提供静态的资源。

(2)ActionMapper

上边已经提到过,ActionMapper提供了HTTP请求与action执行之间的映射,简单的说,ActionMapper会判断这个请求是否应该被Struts2处理,如果需要Struts2处理的话,ActionMapper会返回一个对象来描述请求对应的ActionInvocation的信息。
官方文档解释:The ActionMapper interface provides a mapping between HTTP requests and action invocation requests and vice-versa(反之亦然).

下边是接口的API
这里写图片描述

public interface ActionMapper {/*** Expose the ActionMapping for the current request* 暴露ActionMapping给当前的request* @param request The servlet request* @param configManager The current configuration manager* @return The appropriate action mapping*/ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager);/*** Expose the ActionMapping for the specified action name** @param actionName The name of the action that may have other information embedded in it* @return The appropriate action mapping* @since 2.1.1*/ActionMapping getMappingFromActionName(String actionName);/*** Convert an ActionMapping into a URI string** @param mapping The action mapping* @return The URI string that represents this mapping*/String getUriFromActionMapping(ActionMapping mapping);
}

这里的getMapping(HttpServletRequest request, ConfigurationManager configManager) 就是我们在(1)中提到过的。

(3)ActionProxy

是一个特别的中间层,位于Action和xwork之间,使得在将来有机会引入更多的实现方式,比如通过WebService来实现等。

(4)ConfigurationManager

是xwork配置的管理中心,通俗地讲,可以把它看做是struts.xml这个配置文件在内存中的对应,在服务器启动的时候可以理解为是ConfigurationManager讲struts.xml文件加载到内存中去的。

(5)struts.xml

是struts2的应用配置文件,负责注入URL与Action之间映射的配置,以及执行后页面跳转的Result配置等。

(6)ActionInvocation

真正调用并执行Action,他拥有一个Action实例和这个Action所依赖的拦截器实例。ActionInvocation会执行这些拦截器,Action以及相应的Result,ActionInvocation对象描述了Action运行的整个过程。

(7)Interceptor

拦截器是一些无状态的类,拦截器可以自动拦截Action,他们给开发者提供了在Action运行之前或Resule运行之后来执行一些功能代码的机会,类似于我们熟悉的filter。

(8)Action

动作类是Struts2中的动作执行单元。用来处理用户的请求,并封装业务所需要的数据。

(9)Result

就是不同视图类型的抽象封装模型,不同的视图类型会对应不同的Result实现。

Struts2的运行流程

请参见下一篇文章:
http://blog.csdn.net/xlgen157387/article/details/45845691

这篇关于Struts2系统结构及运行原理(1)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/997174

相关文章

eclipse运行springboot项目,找不到主类

解决办法尝试了很多种,下载sts压缩包行不通。最后解决办法如图: help--->Eclipse Marketplace--->Popular--->找到Spring Tools 3---->Installed。

22.手绘Spring DI运行时序图

1.依赖注入发生的时间 当Spring loC容器完成了 Bean定义资源的定位、载入和解析注册以后,loC容器中已经管理类Bean 定义的相关数据,但是此时loC容器还没有对所管理的Bean进行依赖注入,依赖注入在以下两种情况 发生: 、用户第一次调用getBean()方法时,loC容器触发依赖注入。 、当用户在配置文件中将<bean>元素配置了 lazy-init二false属性,即让

21.手绘Spring IOC运行时序图

1.再谈IOC与 DI IOC(lnversion of Control)控制反转:所谓控制反转,就是把原先我们代码里面需要实现的对象创 建、依赖的代码,反转给容器来帮忙实现。那么必然的我们需要创建一个容器,同时需要一种描述来让 容器知道需要创建的对象与对象的关系。这个描述最具体表现就是我们所看到的配置文件。 DI(Dependency Injection)依赖注入:就是指对象是被动接受依赖类

java中查看函数运行时间和cpu运行时间

android开发调查性能问题中有一个现象,函数的运行时间远低于cpu执行时间,因为函数运行期间线程可能包含等待操作。native层可以查看实际的cpu执行时间和函数执行时间。在java中如何实现? 借助AI得到了答案 import java.lang.management.ManagementFactory;import java.lang.management.Threa

数据库原理与安全复习笔记(未完待续)

1 概念 产生与发展:人工管理阶段 → \to → 文件系统阶段 → \to → 数据库系统阶段。 数据库系统特点:数据的管理者(DBMS);数据结构化;数据共享性高,冗余度低,易于扩充;数据独立性高。DBMS 对数据的控制功能:数据的安全性保护;数据的完整性检查;并发控制;数据库恢复。 数据库技术研究领域:数据库管理系统软件的研发;数据库设计;数据库理论。数据模型要素 数据结构:描述数据库

计算机组成原理——RECORD

第一章 概论 1.固件  将部分操作系统固化——即把软件永恒存于只读存储器中。 2.多级层次结构的计算机系统 3.冯*诺依曼计算机的特点 4.现代计算机的组成:CPU、I/O设备、主存储器(MM) 5.细化的计算机组成框图 6.指令操作的三个阶段:取指、分析、执行 第二章 计算机的发展 1.第一台由电子管组成的电子数字积分和计算机(ENIAC) 第三章 系统总线

GaussDB关键技术原理:高性能(二)

GaussDB关键技术原理:高性能(一)从数据库性能优化系统概述对GaussDB的高性能技术进行了解读,本篇将从查询处理综述方面继续分享GaussDB的高性能技术的精彩内容。 2 查询处理综述 内容概要:本章节介绍查询端到端处理的执行流程,首先让读者对查询在数据库内部如何执行有一个初步的认识,充分理解查询处理各阶段主要瓶颈点以及对应的解决方案,本章以GaussDB为例讲解查询执行的几个主要阶段

如何实现一台机器上运行多个MySQL实例?

在一台机器上一个MySQL服务器运行多个MySQL实例有什么好处?这里我先入为主给大家介绍这样做至少存在两个好处(看完这篇文章后理解会更透彻): (1)减轻服务器链接负担 (2)为不同的用户提供不同的mysqld服务器的访问权限以方便这些用户进行自我管理。   下面我介绍具体的实现过程: 一、准备工作     台式机一台、Windows系统、MySQL服务器(我安装的版本是MySQL

Python几种建表方法运行时间的比较

建立一个表[0,1,2,3.......10n],下面几种方法都能实现,但是运行时间却截然不同哦 import time#方法一def test1(n):list=[]for i in range(n*10):list=list+[i]return list#方法二def test2(n):list=[]for i in range(n*10):list.append(i)#方法三d

【计算机组成原理】部分题目汇总

计算机组成原理 部分题目汇总 一. 简答题 RISC和CICS 简要说明,比较异同 RISC(精简指令集)注重简单快速的指令执行,使用少量通用寄存器,固定长度指令,优化硬件性能,依赖软件(如编译器)来提升效率。 CISC(复杂指令集)包含多样复杂的指令,能一条指令完成多步操作,采用变长指令,减少指令数但可能增加执行时间,倾向于硬件直接支持复杂功能减轻软件负担。 两者均追求高性能,但RISC