You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Apache Struts is a free, open-source, MVC framework for creating elegant, modern Java web applications. It favors convention over configuration, is extensible using a plugin architecture, and ships with plugins to support REST, AJAX and JSON.
publicinterfaceStrutsStatics {
/** * Constant for the HTTP request object. */publicstaticfinalStringHTTP_REQUEST = "com.opensymphony.xwork2.dispatcher.HttpServletRequest";
...
...
/** * Set as an attribute in the request to let other parts of the framework know that the invocation is happening inside an * action tag */publicstaticfinalStringSTRUTS_ACTION_TAG_INVOCATION= "struts.actiontag.invocation";
}
interfaceInternalFactory<T> extendsSerializable {
/** * Creates an object to be injected. * * @param context of this injection * @return instance to be injected */Tcreate(InternalContextcontext);
}
publicbooleanexecuteStaticResourceRequest(HttpServletRequestrequest, HttpServletResponseresponse) throwsIOException, ServletException {
// there is no action in this request, should we look for a static resource?StringresourcePath = RequestUtils.getServletPath(request);
if ("".equals(resourcePath) && null != request.getPathInfo()) {
resourcePath = request.getPathInfo();
}
StaticContentLoaderstaticResourceLoader = dispatcher.getContainer().getInstance(StaticContentLoader.class);
if (staticResourceLoader.canHandle(resourcePath)) {
staticResourceLoader.findStaticResource(resourcePath, request, response);
// The framework did its job herereturntrue;
} else {
// this is a normal request, let it pass throughreturnfalse;
}
}
/** * Executes an action * @throws ServletException */publicvoidexecuteAction(HttpServletRequestrequest, HttpServletResponseresponse, ActionMappingmapping) throwsServletException {
dispatcher.serviceAction(request, response, mapping);
}
运行主线
入口程序
StrutsPrepareAndExecuteFilter是Struts2的入口点,实现了Filter和StrutsStatics接口,其中StrutsStatics定义了一些常量
而实现了Filter接口,让Struts2能够过滤请求,如静态资源、Servlet等,在doFilter()方法中实现过滤逻辑,而init()方法会在且只在Filter被初始化的时候被调用一次,让我们来看看StrutsPrepareAndExecuteFilter的init()方法
初始化核心分发器:Dispatcher
init()方法主要是对Dispatcher,PrepareOperations,ExecuteOperations三个类进行初始化,其中Dispatcher在Struts2中占有很重要的地位,无论是初始化Struts2还是对HTTP请求的处理,同时也架起了Struts2和XWork之间的一道桥梁,因此我们先深入
dispatcher = init.initDispatcher(config);
这段代码看一看上面的没什么,
dispatcher.init();
才是重头戏,继续深入ConfigurationProvider
所有的初始化方法都是以init_开头,其核心只不过是调用configurationManager#addContainerProvider()方法,那到底什么是配置元素加载器(ContainerProvider)呢,比如init_DefaultProperties(),看一下DefaultPropertiesProvider的继承关系
其他的ContainerProvider也都实现了ConfigurationProvider这个接口,我们知道Struts2的配置文件形式有很多种,比如.xml,.properties等,所以Struts2就定义了ConfigurationProvider这个统一的接口,让框架支持处理所有的配置形式,而每一个ContainerProvider的实现类都可以根据不同的配置文件的特点进行设计。
同时ConfigurationProvider继承了ContainerProvider和PackageProvider两个接口,ContainerProvider的子类有:FileManagerFactoryProvider,StubConfigurationProvider, XmlConfigurationProvider, BeanSelectionProvider等,它的用途大概就是处理诸如XML,Properties等格式的配置文件。而PackageProvider的操作对象是PackageConfig,从源码可以看出,PackageConfig对应了XML配置文件中的package节点,这样PackageProvider的作用也不言而喻
初始化容器
Struts2中的所有内置对象都会交给Container去管理,比如XML中的Bean,Constant节点以及Properties文件中的参数,Container的实现类会扫描
@Inject
注解,进行依赖注入,下面我们看看Container container = init_PreloadConfiguration();
这行代码做了什么事情我们再看看Container的实现类ContainerImpl,它的内部缓存了两个实例factories和factoryNamesByType,而factories根据Key缓存了不同对象的制造工厂,Key中有两个变量:type,name,factoryNamesByType则在factories基础之上根据名称进行寻址。
getInstance()方法的每次调用,都会根据传进来的type,class构造一个Key对象,然后到factories中查找到对应的工厂类,调用Factory的create()方法,创建对象
PrepareOperations和ExecuteOperations分析
从源码中可以看出,PrepareOperations负责创建ActionContext,清理Request,设置编码等
ExecuteOperations则只有两个方法,负责真正的执行操作,executeStaticResourceRequest()负责静态资源,executeAction()是一个代理方法,将真正的执行交给Dispatcher.serviceAction()方法
总结
(1) 封装FilterConfig->FilterHostConfig
(2) 初始化日志操作
(3) 初始化Dispatcher
(4) 初始化PrepareOperations和ExecuteOperations
The text was updated successfully, but these errors were encountered: