struts2 实现原理解析
2013-08-08 14:29
239 查看
struts2 实现原理解析
struts2 操作 action servlet,是通过在web.xml中配置一个filter实现的<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
而这个Filter FilterDispatcher 实际做了下列工作
public class FilterDispatcher implements StrutsStatics, Filter
1.filter的init()方法建立一个Dispatcher对象
以后struts2 都是围绕这个Dispatcher对象进行操作
init()方法将参数FilterConfig对象传给Dispatcher对象,实际就是将ServletContext对象传递给Dispatcher对象
而ServletContext包含了各种基本Servlet的运行环境,比如ServletContext提供了getAttribute(), setAttribute() 等方法
所以,实际是Dispatcher对象接管了基本Servlet的一切功能
public void init(FilterConfig filterConfig) throws ServletException { dispatcher = createDispatcher(filterConfig); ... } |
protected Dispatcher createDispatcher(FilterConfig filterConfig) { ... return new Dispatcher(filterConfig.getServletContext(), params); } |
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; ServletContext servletContext = getServletContext(); .... dispatcher.serviceAction(request, response, servletContext, mapping); } |
1.它是线程安全的,即Dispatcher对象支持多线程,且每线程一副本
public class Dispatcher { private static ThreadLocal<Dispatcher> instance = new ThreadLocal<Dispatcher>(); //Store the dispatcher instance for this thread. public static void setInstance(Dispatcher instance) { Dispatcher.instance.set(instance); // Tie the ObjectFactory threadlocal instance to this Dispatcher instance if (instance != null) { Container cont = instance.getContainer(); if (cont != null) { ObjectFactory.setObjectFactory(cont.getInstance(ObjectFactory.class)); } else { LOG.warn("This dispatcher instance doesn't have a container, so the object factory won't be set."); } } else { ObjectFactory.setObjectFactory(null); } } //Provide the dispatcher instance for the current thread. public static Dispatcher getInstance() { return instance.get(); } |
private ServletContext servletContext; private Map<String, String> initParams; public Dispatcher(ServletContext servletContext, Map<String, String> initParams) { this.servletContext = servletContext; this.initParams = initParams; } |
这个serviceAction()方法,就是整个Strtus2的主引擎.
serviceAction内部完成了多项功能:
配置文件加载,配置初始化
调用ActionProxy对象实现对Action类的执行
ActionProxy对象又会按照Struts2的Stack结构依次执行Inteceptor,action,method
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,ActionMapping mapping) throws ServletException { Map<String, Object> extraContext = createContextMap(request, response, mapping, context); // If there was a previous value stack, then create a new copy and pass it in to be used by the new Action ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY); if (stack != null) { extraContext.put(ActionContext.VALUE_STACK, ValueStackFactory.getFactory().createValueStack(stack)); } try { String namespace = mapping.getNamespace(); String name = mapping.getName(); String method = mapping.getMethod(); Configuration config = configurationManager.getConfiguration(); ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy( namespace, name, extraContext, true, false); proxy.setMethod(method); request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack()); // if the ActionMapping says to go straight to a result, do it! if (mapping.getResult() != null) { Result result = mapping.getResult(); result.execute(proxy.getInvocation()); } else { proxy.execute(); } // If there was a previous value stack then set it back onto the request if (stack != null) { request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack); } } catch (ConfigurationException e) { } } |
相关文章推荐
- struts2 实现原理解析 此博文包含图片 (2010-07-27 14:49:36)转载▼ 标签: filter dispatcher threadlocal actionproxy 杂谈 分
- struts2 实现原理解析
- struts2 实现原理解析
- struts2 实现原理解析
- struts2--(4)---深入解析struts.xml配置的实现原理
- struts2 实现原理解析
- struts2拦截器的实现原理及源码解析
- struts2 实现原理解析
- Shiro整合SSH开发2:结合Struts2实现登陆和退出以及Shiro执行流程和原理解析
- Spring AOP的实现原理之<aop:aspectj-autoproxy />的解析
- 数据库水平切分的实现原理解析---分库,分表,主从,集群,负载均衡器
- DB 数据库水平切分的实现原理解析---分库,分表,主从,集群,负载均衡器
- Dubbo原理解析-Dubbo内核实现之SPI简单介绍
- Android 带你从源码的角度解析Scroller的滚动实现原理
- URL短地址压缩算法 微博短地址原理解析 (Java实现)
- 【React Native】从源码一步一步解析它的实现原理
- 秋色园QBlog技术原理解析:博客一键安装工具技术实现[附源码下载]
- Ogre阴影实现原理解析
- 全面解析SpringBoot自动配置的实现原理
- Skinned Mesh 原理解析和一个最简单的实现示例