Struts2源码分析--请求处理
?
?? ? ? ?请求首先通过Filter chain,Filter主要包括ActionContextCleanUp,它主要清理当前线程的ActionContext和Dispatcher;FilterDispatcher主要通过AcionMapper来决定需要调用哪个Action。?? ? ? ?ActionMapper取得了ActionMapping后,在Dispatcher的serviceAction方法里创建ActionProxy,ActionProxy创建ActionInvocation,然后ActionInvocation调用Interceptors,执行Action本身,创建Result并返回,当然,如果要在返回之前做些什么,可以实现PreResultListener。
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来解析,
Struts2请求流程1、客户端发送请求2、请求先通过ActionContextCleanUp-->FilterDispatcher3、FilterDispatcher通过ActionMapper来决定这个Request需要调用哪个Action4、如果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(2.1.2)部分源码阅读?? ?从org.apache.struts2.dispatcher.FilterDispatcher开始
??? ??顺着流程我们看Disptcher的init方法。init方法里就是初始读取一些配置文件等,先看init_DefaultProperties,主要是读取properties配置文件。??? ?再来看init_TraditionalXmlConfigurations方法,这个是读取struts-default.xml和Struts.xml的方法。?? ??好了,action执行完了,还要根据ResultConfig返回到view,也就是在invoke方法中调用executeResult方法。?? ? 最后补充一下,Struts2的查找值和设置值都是使用Ognl来实现的。关于Ognl的介绍可以到其官方网站查看http://www.ognl.org/,我在网上也找到另外一篇http://www.javaeye.com/topic/254684和http://www.javaeye.com/topic/223612。完了来看下面这段小测试程序(其它的Ognl的测试可以自己添加)。public class TestOgnl { private User user; private Map context; @Before public void setUp() throws Exception { } @Test public void ognlGetValue() throws Exception { reset(); Assert.assertEquals("myyate", Ognl.getValue("name", user)); Assert.assertEquals("cares", Ognl.getValue("dept.name", user)); Assert.assertEquals("myyate", Ognl.getValue("name", context, user)); Assert.assertEquals("contextmap", Ognl.getValue("#name", context, user)); Assert.assertEquals("parker", Ognl.getValue("#pen", context, user)); } @Test public void ognlSetValue() throws Exception { reset(); Ognl.setValue("name", user, "myyateC"); Assert.assertEquals("myyateC", Ognl.getValue("name", user)); Ognl.setValue("dept.name", user, "caresC"); Assert.assertEquals("caresC", Ognl.getValue("dept.name", user)); Assert.assertEquals("contextmap", Ognl.getValue("#name", context, user)); Ognl.setValue("#name", context, user, "contextmapC"); Assert.assertEquals("contextmapC", Ognl.getValue("#name", context, user)); Assert.assertEquals("parker", Ognl.getValue("#pen", context, user)); Ognl.setValue("#name", context, user, "parkerC"); Assert.assertEquals("parkerC", Ognl.getValue("#name", context, user)); } public static void main(String[] args) throws Exception { JUnitCore.runClasses(TestOgnl.class); } private void reset() { user = new User("myyate", new Dept("cares")); context = new OgnlContext(); context.put("pen", "parker"); context.put("name", "contextmap"); }}class User { public User(String name, Dept dept) { this.name = name; this.dept = dept; } String name; private Dept dept; public Dept getDept() { return dept; } public String getName() { return name; } public void setDept(Dept dept) { this.dept = dept; } public void setName(String name) { this.name = name; }}class Dept { public Dept(String name) { this.name = name; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}?? ??这样,一个Struts2的请求流程基本上就结束了。其实我觉得做项目把Struts2参考文档看两遍就可以了,呵呵!(写博客比看代码还累)