您的位置:首页 > 编程语言 > Java开发

struts2 原理介绍 笔记

2013-01-16 23:11 134 查看

主要的包

org.apache.struts2.component 视图组件,如tokent,tree

org.apache.struts2.config 与配置相关的接口和类

org.apahce.struts2.dispatcher struts2的核心包

org.apache.struts.impl 只定义了三个类:StrutsActionProxy StrutsActionProxyFactory StrutsObjectFactory,这三个类是对xwork的扩展

org.apache.struts2.interceptor 定义内置的拦截器

org.apache.struts2.util 工具包

org.apache.struts2.validators 定义类:DWRValidator

org.apache.struts2.views 执行freemarker、jsp、velocy等不同的页面呈现

初始化

对各种配置信息xml或者properties统一转化为java对象处理

核心类

名称
javaPackage
描述
Container
xwork2.inject
容器定义接口,对象管理基础构建
ContainerImpl
xwork2.inject
实现类,提供对象声明周期管理和依赖注入功能
PackageConfig
xwork2.config.entities
定义 xml package 节点 对应的数据结构
ConfigurationProvider
xwork2.config
配置加载的统一接口,加载Container和PackageConfig
ContainerProvider
Xwork2.config
Container的配置加载接口,实现类需要初始化容器内所有对象
PackageProvider
Xowrk2.config
PackageConfig的配置加载接口
ContainerBuilder
Xowrk2.inject
初始化时构造容器
PackageConfigProvidier
PackageConfig内部类
初始化时构造PackageProvider
ConfigurationManager
Xwork2.config
操作代理类,包含了所有ContainerProvider和PackageProvider的实现
Configurations
Xwork2.config
配置数据的管理类,运行时获取配置的基本接口

调用图



说明

FilterDispatcher

实现了FIlter接口和StrutsStatics接口

当容器启动时调用FilterDispatcher.init(FilterConfig filterConfig)方法。该方法创建Dispatcher对象,并根据FilterConfig初始化该对象。

请求处理

核心类

名称
javaPackage
描述
FilterDispatcher
Struts2.dispatcher
过滤器,执行Action,清理ActionContext避免内存泄露, 处理静态内容,为请求启动xwork的拦截器
Dispatcher
Struts2.dispatcher
核心分发器,http请求处理的实际场所
PrepareOperations
Struts2.dispatcher.ng
http请求预处理
ExecuteOperations
Struts2.dispatcher.ng
http请求逻辑处理
ActionProxy
Xwork2
Xwork的入口
ActionInvocation
Xwork2
负责Action 和拦截器的调用
ActionContext
Xwork2
提供xwork处理请求的数据环境
ValueStack
Xowrk2.util
数据环境中提供表达式运算的工具类,也是xwork中进行数据访问的基础
Result
Xwork2
负责输出到视图

调用图



结构图



说明

1. 浏览器提交请求

2. 请求经过一系列的filter(其中的ActionContextCleanUp 可选过滤器,当需要strtus2和其他框架集成时很有帮助,如SiteMesh)

3.FilterDispatcher 询问ActionMapper来决定调用某个Action,将请求交给 ActionProxy处理

调用FilterDispatcher.doFilter()对 request对象进行封装,调用ActionMapper.getMapping得到 ActionMapping对象,该对象包括三个参数:Action的name namespace method。 如果返回的ActionMapping对象为null,则认为用户请求不是Action,如果该请求已struts开头,会查找在web.xml中
FilterDispatcher中 package参数配置的路径,将其中内容直接在页面上显示,如txt文件。

FilterDispatcher.findStatcisResource()查找静态资源

如果返回ActionMapping对象,则调用Dispatcher.serviceAction()

Dispatcher.serviceAction() 加载struts的配置文件,如struts-default.xml struts-plugin.xml struts.xml,并保存在xwork2.config.entities.XxxxConfig类中。

4.ActionProxy通过 ConfigurationManager访问配置文件,找到Action对应的类和方法。

类 XmlConfigurationProvider负 责配置文件的读取和解析, addAction 负责读取 action 标签,并把数据保存在ActionConfig中; addResultType() 将result-type标签转换为ResultTypeConfig对象;拦截器, addPackage等。

配置信息加载完后,通过ActionProxyFactory 创建Action的代理对象 ActionProxy。

ActionProxy 是Action的执行起始点,它的实现是DefaultActionProxy和DefaultActionProxyFactory

5.ActionProxy创建ActionInvocation的实例

ActionInvocation标示Action的执行状态,它的实现是DefaultActionInvocation。 该类的invoke() 方法实现了拦截器的递归调用和执行Action的execute() 方法

6. ActionInvocation使用命令模式来调用,在调用Action前后调用Interceptor

自定义Interceptor继承 AbstractInterceptor

ActionProxy————》ActionInvocation——》Action

7. 当action执行完毕,ActionInvocation根据配置文件找到对应的返回结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐