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

相关文章

通过prometheus监控Tomcat运行状态的操作流程

《通过prometheus监控Tomcat运行状态的操作流程》文章介绍了如何安装和配置Tomcat,并使用Prometheus和TomcatExporter来监控Tomcat的运行状态,文章详细讲解了... 目录Tomcat安装配置以及prometheus监控Tomcat一. 安装并配置tomcat1、安装

MySQL中的MVCC底层原理解读

《MySQL中的MVCC底层原理解读》本文详细介绍了MySQL中的多版本并发控制(MVCC)机制,包括版本链、ReadView以及在不同事务隔离级别下MVCC的工作原理,通过一个具体的示例演示了在可重... 目录简介ReadView版本链演示过程总结简介MVCC(Multi-Version Concurr

mysqld_multi在Linux服务器上运行多个MySQL实例

《mysqld_multi在Linux服务器上运行多个MySQL实例》在Linux系统上使用mysqld_multi来启动和管理多个MySQL实例是一种常见的做法,这种方式允许你在同一台机器上运行多个... 目录1. 安装mysql2. 配置文件示例配置文件3. 创建数据目录4. 启动和管理实例启动所有实例

IDEA运行spring项目时,控制台未出现的解决方案

《IDEA运行spring项目时,控制台未出现的解决方案》文章总结了在使用IDEA运行代码时,控制台未出现的问题和解决方案,问题可能是由于点击图标或重启IDEA后控制台仍未显示,解决方案提供了解决方法... 目录问题分析解决方案总结问题js使用IDEA,点击运行按钮,运行结束,但控制台未出现http://

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

Redis主从/哨兵机制原理分析

《Redis主从/哨兵机制原理分析》本文介绍了Redis的主从复制和哨兵机制,主从复制实现了数据的热备份和负载均衡,而哨兵机制可以监控Redis集群,实现自动故障转移,哨兵机制通过监控、下线、选举和故... 目录一、主从复制1.1 什么是主从复制1.2 主从复制的作用1.3 主从复制原理1.3.1 全量复制

Redis主从复制的原理分析

《Redis主从复制的原理分析》Redis主从复制通过将数据镜像到多个从节点,实现高可用性和扩展性,主从复制包括初次全量同步和增量同步两个阶段,为优化复制性能,可以采用AOF持久化、调整复制超时时间、... 目录Redis主从复制的原理主从复制概述配置主从复制数据同步过程复制一致性与延迟故障转移机制监控与维

SpringCloud配置动态更新原理解析

《SpringCloud配置动态更新原理解析》在微服务架构的浩瀚星海中,服务配置的动态更新如同魔法一般,能够让应用在不重启的情况下,实时响应配置的变更,SpringCloud作为微服务架构中的佼佼者,... 目录一、SpringBoot、Cloud配置的读取二、SpringCloud配置动态刷新三、更新@R

Redis主从复制实现原理分析

《Redis主从复制实现原理分析》Redis主从复制通过Sync和CommandPropagate阶段实现数据同步,2.8版本后引入Psync指令,根据复制偏移量进行全量或部分同步,优化了数据传输效率... 目录Redis主DodMIK从复制实现原理实现原理Psync: 2.8版本后总结Redis主从复制实