StrutsPrepareAndExecuteFilter作用详解

StrutsPrepareAndExecuteFilter作用详解 

1 目的

了解StrutsPrepareAndExecuteFilter的作用

2 作用

2.1 基本介绍

1)FilterDispatcher是早期struts2的过滤器,后期的都用StrutsPrepareAndExecuteFilter了。

         FilterDispatcher是struts2.0.x到2.1.2版本的核心过滤器。

         StrutsPrepareAndExecuteFilter是自2.1.3开始就替代了FilterDispatcher的。

(2StrutsPrepareAndExecuteFilter名字可知,prepare与execute,前者表示准备,可以说是指filter中的init方法,即配制的导入;后者表示进行过滤,指doFilter方法,即将request请求,转发给对应的 action去处理。

         在使用struts的时候要在web.xml中配置一个过滤器,来拦截用户发起的请求,并进行一些预处理,根据配置文件把请求分配给对应的action并将请求中的参数与action中的字段进行对应赋值。

(3)在StrutsPrepareAndExecuteFilter过滤器中包含相应的功能。三个初始化参数:  

         1. config参数:指定要加载的配置文件。逗号分割。

         2. actionPackages参数:指定Action类所在的包空间。逗号分割。

         3. configProviders参数:自定义配置文件提供者,需要实现ConfigurationProvider接口类。逗号分割。

(4)要构建一个过滤器很简单就是实现javax.servlet.Filter接口即可。这个接口有三个方法:

          1. init() :这个方法在容器实例化过滤器时被调用,它主要设计用于使过滤器为处理做准备。该方法接受一个 FilterConfig 类型的对象作为输入。

          2. doFilter() :与 servlet 拥有一个 service() 方法(这个方法又调用 doPost() 或者 doGet() )来处理请求一样,过滤器拥有单个用于处理请求和响应的方法doFilter() 。这个方法接受三个输入参数:一个 ServletRequest 、 response 和一个 FilterChain 对象。

          3. destroy() :正如您想像的那样,这个方法执行任何清理操作,这些操作可能需要在自动垃圾收集之前进行。

2.2 源码剖析

1. 概述

     Struts2的核心是一个Filter,Action可以脱离web容器,那么是什么让http请求和action关联在一起的,下面我们深入StrutsPrepareAndExecuteFilter源码来分析下Struts2是如何工作的。

2. 源码属性方法简介

 

3. 源码剖析

 

1、init方法

         init是Filter第一个运行的方法,我们看下struts2的核心Filter在调用init方法初始化时做哪些工作:

  1. public void init(FilterConfig filterConfig) throws ServletException {  
  2.         InitOperations init = new InitOperations();  
  3.         try {  
  4.             //封装filterConfig,其中有个主要方法getInitParameterNames将参数名字以String格式存储在List中  
  5.             FilterHostConfig config = new FilterHostConfig(filterConfig);  
  6.             // 初始化struts内部日志  
  7.            init.initLogging(config);  
  8.             //<strong>创建dispatcher ,并初始化,这部分下面我们重点分析,初始化时加载那些资源</strong>  
  9.             Dispatcher dispatcher = init.initDispatcher(config);  
  10.             init.initStaticContentLoader(config, dispatcher);  
  11.             //初始化类属性:prepare 、execute   
  12.             prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);  
  13.             execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);  
  14.             this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);  
  15.             //回调空的postInit方法  
  16.             postInit(dispatcher, filterConfig);  
  17.         } finally {  
  18.             init.cleanup();  
  19.         }  
  20.  }  
首先看下FilterHostConfig ,源码如下:

  1. public class FilterHostConfig implements HostConfig {  
  2.   
  3.     private FilterConfig config;  
  4.     /** 
  5.      *构造函数   
  6.      */      
  7.     public FilterHostConfig(FilterConfig config) {  
  8.         this.config = config;  
  9.     }  
  10.     /** 
  11.      *  根据init-param配置的param-name获取param-value的值 
  12.      */    
  13.     public String getInitParameter(String key) {  
  14.         return config.getInitParameter(key);  
  15.     }  
  16.        /** 
  17.          *  返回初始化参数名的List 
  18.      */   
  19.     public Iterator<String> getInitParameterNames() {  
  20.         return MakeIterator.convert(config.getInitParameterNames());  
  21.     }  
  22.   
  23.     public ServletContext getServletContext() {  
  24.         return config.getServletContext();  
  25.     }  
  26. }  

只有短短的几行代码,getInitParameterNames是这个类的核心,将Filter初始化参数名称有枚举类型转为Iterator。此类的主要作为是对filterConfig 封装。

2 重点来了,创建并初始化Dispatcher 

  1. public Dispatcher initDispatcher( HostConfig filterConfig ) {  
  2.        Dispatcher dispatcher = createDispatcher(filterConfig);  
  3.        dispatcher.init();  
  4.        return dispatcher;  
  5.    }  

 创建Dispatcher,会读取 filterConfig 中的配置信息,将配置信息解析出来,封装成为一个Map,然后根绝servlet上下文和参数Map构造Dispatcher :

  1. private Dispatcher createDispatcher( HostConfig filterConfig ) {  
  2.         Map<String, String> params = new HashMap<String, String>();  
  3.         for ( Iterator e = filterConfig.getInitParameterNames(); e.hasNext(); ) {  
  4.             String name = (String) e.next();  
  5.             String value = filterConfig.getInitParameter(name);  
  6.             params.put(name, value);  
  7.         }  
  8.         return new Dispatcher(filterConfig.getServletContext(), params);  
  9.     }  
 Dispatcher初始化,加载struts2的相关配置文件,将按照顺序逐一加载:default.properties,struts-default.xml,struts-plugin.xml,struts.xml,……

  1. /** 
  2. *初始化过程中依次加载如下配置文件 
  3. */  
  4. public void init() {  
  5.   
  6.         if (configurationManager == null) {  
  7.             configurationManager = new ConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);  
  8.         }  
  9.   
  10.         try {  
  11.             //加载org/apache/struts2/default.properties  
  12.             init_DefaultProperties(); // [1]  
  13.            //加载struts-default.xml,struts-plugin.xml,struts.xml  
  14.             init_TraditionalXmlConfigurations(); // [2]  
  15.             init_LegacyStrutsProperties(); // [3]  
  16.            //用户自己实现的ConfigurationProviders类              
  17.             init_CustomConfigurationProviders(); // [5]  
  18.             //Filter的初始化参数  
  19.             init_FilterInitParameters() ; // [6]  
  20.             init_AliasStandardObjects() ; // [7]  
  21.   
  22.             Container container = init_PreloadConfiguration();  
  23.             container.inject(this);  
  24.             init_CheckConfigurationReloading(container);  
  25.             init_CheckWebLogicWorkaround(container);  
  26.   
  27.             if (!dispatcherListeners.isEmpty()) {  
  28.                 for (DispatcherListener l : dispatcherListeners) {  
  29.                     l.dispatcherInitialized(this);  
  30.                 }  
  31.             }  
  32.         } catch (Exception ex) {  
  33.             if (LOG.isErrorEnabled())  
  34.                 LOG.error("Dispatcher initialization failed", ex);  
  35.             throw new StrutsException(ex);  
  36.         }  
  37.     }  

初始化default.properties,具体的初始化操作在DefaultPropertiesProvider类中

  1. private void init_DefaultProperties() {  
  2.        configurationManager.addConfigurationProvider(new DefaultPropertiesProvider());  
  3.    }  

下面我们看下DefaultPropertiesProvider类源码:

  1. public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {  
  2.           
  3.         Settings defaultSettings = null;  
  4.         try {  
  5.             defaultSettings = new PropertiesSettings("org/apache/struts2/default");  
  6.         } catch (Exception e) {  
  7.             throw new ConfigurationException("Could not find or error in org/apache/struts2/default.properties", e);  
  8.         }  
  9.           
  10.         loadSettings(props, defaultSettings);  
  11.     }  
 其他的我们再次省略,大家可以浏览下各个初始化操作都加载了那些文件

3、doFilter方法

     doFilter是过滤器的执行方法,它拦截提交的HttpServletRequest请求,HttpServletResponse响应,作为strtus2的核心拦截器,在doFilter里面到底做了哪些工作,我们将逐行解读其源码,源码如下:

  1. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {  
  2.      //父类向子类转:强转为http请求、响应  
  3.      HttpServletRequest request = (HttpServletRequest) req;  
  4.      HttpServletResponse response = (HttpServletResponse) res;  
  5.   
  6.      try {  
  7.          //设置编码和国际化  
  8.          prepare.setEncodingAndLocale(request, response);  
  9.           //创建Action上下文(重点)  
  10.          prepare.createActionContext(request, response);  
  11.          prepare.assignDispatcherToThread();  
  12. if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {  
  13.     chain.doFilter(request, response);  
  14. else {  
  15.     request = prepare.wrapRequest(request);  
  16.     ActionMapping mapping = prepare.findActionMapping(request, response, true);  
  17.     if (mapping == null) {  
  18.         boolean handled = execute.executeStaticResourceRequest(request, response);  
  19.         if (!handled) {  
  20.             chain.doFilter(request, response);  
  21.         }  
  22.     } else {  
  23.         execute.executeAction(request, response, mapping);  
  24.     }  
  25. }  
  26.      } finally {  
  27.          prepare.cleanupRequest(request);  
  28.      }  
  29.  }  

 setEncodingAndLocale调用了dispatcher方法的prepare方法:

  1. /** 
  2.      * Sets the request encoding and locale on the response 
  3.      */  
  4.     public void setEncodingAndLocale(HttpServletRequest request, HttpServletResponse response) {  
  5.         dispatcher.prepare(request, response);  
  6.     }  

下面我们看下prepare方法,这个方法很简单只是设置了encoding 、locale ,做的只是一些辅助的工作:

  1. public void prepare(HttpServletRequest request, HttpServletResponse response) {  
  2.         String encoding = null;  
  3.         if (defaultEncoding != null) {  
  4.             encoding = defaultEncoding;  
  5.         }  
  6.   
  7.         Locale locale = null;  
  8.         if (defaultLocale != null) {  
  9.             locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale());  
  10.         }  
  11.   
  12.         if (encoding != null) {  
  13.             try {  
  14.                 request.setCharacterEncoding(encoding);  
  15.             } catch (Exception e) {  
  16.                 LOG.error("Error setting character encoding to '" + encoding + "' - ignoring.", e);  
  17.             }  
  18.         }  
  19.   
  20.         if (locale != null) {  
  21.             response.setLocale(locale);  
  22.         }  
  23.   
  24.         if (paramsWorkaroundEnabled) {  
  25.             request.getParameter("foo"); // simply read any parameter (existing or not) to "prime" the request  
  26.         }  
  27.     }  

Action上下文创建(重点)

       ActionContext是一个容器,这个容易主要存储request、session、application、parameters等相关信息.ActionContext是一个线程的本地变量,这意味着不同的action之间不会共享ActionContext,所以也不用考虑线程安全问题。其实质是一个Map,key是标示request、session、……的字符串,值是其对应的对象:

  1. static ThreadLocal actionContext = new ThreadLocal();  
  2. Map<String, Object> context; 

下面我们看下如何创建action上下文的,代码如下:

  1. /** 
  2. *创建Action上下文,初始化thread local 
  3. */  
  4. public ActionContext createActionContext(HttpServletRequest request, HttpServletResponse response) {  
  5.     ActionContext ctx;  
  6.     Integer counter = 1;  
  7.     Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);  
  8.     if (oldCounter != null) {  
  9.         counter = oldCounter + 1;  
  10.     }  
  11.     //注意此处是从ThreadLocal中获取此ActionContext变量  
  12.     ActionContext oldContext = ActionContext.getContext();  
  13.     if (oldContext != null) {  
  14.         // detected existing context, so we are probably in a forward  
  15.         ctx = new ActionContext(new HashMap<String, Object>(oldContext.getContextMap()));  
  16.     } else {  
  17.         ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();  
  18.         stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));  
  19.         //stack.getContext()返回的是一个Map<String,Object>,根据此Map构造一个ActionContext  
  20.         ctx = new ActionContext(stack.getContext());  
  21.     }  
  22.     request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);  
  23.     //将ActionContext存如ThreadLocal  
  24.     ActionContext.setContext(ctx);  
  25.     return ctx;  
  26. }  


  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值