zoukankan      html  css  js  c++  java
  • StrutsPrepareAndExecuteFilter

    StrutsPrepareAndExecuteFilter的作用

    FilterDispatcher是早期struts2的过滤器,后期的都用StrutsPrepareAndExecuteFilter了,如 2.1.6、2.1.8。StrutsPrepareAndExecuteFilter名字已经很能说明问题了,prepare与execute,前者表示准备,可以说是指filter中的init方法,即配制的导入;后者表示进行过滤,指doFilter方法,即将request请求,转发给对应的 action去处理。


    FilterDispatcher是struts2.0.x到2.1.2版本的核心过滤器.!
    StrutsPrepareAndExecuteFilter是自2.1.3开始就替代了FilterDispatcher的.!

    这样的改革当然是有好处的.!

    为什么这么说.? 应该知道如果我们自己定义过滤器的话, 是要放在strtus2的过滤器之前的, 如果放在struts2过滤器之后,你自己的过滤器对action的过滤作用就废了,不会有效!除非你是访问jsp/html!

    那我现在有需求, 我必须使用Action的环境,而又想在执行action之前拿filter做一些事, 用FilterDispatcher是做不到的.!

    那么StrutsPrepareAndExecuteFilter可以把他拆分成StrutsPrepareFilter和StrutsExecuteFilter,可以在这两个过滤器之间加上我们自己的过滤器.!

    给你打个比喻, 现在有病人要做手术, 现在struts2要做两件事, 搭病床(环境),执行手术.! 那么打麻药的工作呢.? 不可能要病人站着打吧, 所以必须有病床的环境,打完麻药之后再动手术.! 这个比喻非常形象了.!

    如果是2.1.3之前的版本,用org.apache.struts2.dispatcher.FilterDispatcher,

    否则,用org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter。    

     从Struts2.1.3开始,将废弃ActionContextCleanUp过滤器,而在StrutsPrepareAndExecuteFilter过滤器中包含相应的功能。      

    三个初始化参数:    

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

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

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

      

     <filter>  
        <filter-name>struts2</filter-name>  
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
        <init-param>  
            <param-name>actionPackages</param-name>  
            <param-value>com.cjm.web.action</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>struts2</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping> 

    如果程序报类找不到异常,且后面有   org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 

    等信息,可能是你的版本问题.解决方法是确定你的struts版本,如果是2.1.3之前的版本,用 org.apache.struts2.dispatcher.FilterDispatcher,以后的版本则配置org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

    文章乃参考、转载其他博客所得,仅供自己学习作笔记使用!!!
     

    StrutsPrepareAndExecuteFilter和FilterDispatcher的区别比较

    FilterDispatcher是早期struts2的过滤器,如 2.1.3 之前的版本,后期的都用StrutsPrepareAndExecuteFilter了,如 2.1.6、2.1.8。

    StrutsPrepareAndExecuteFilter名字已经很能说明问题了:

      1、prepare与execute,前者表示准备,可以说是指filter中的init方法,即配制的导入;

      2、prepare与execute,后者表示进行过滤,指doFilter方法,即将request请求,转发给对应的 action去处理。

    FilterDispatcher是struts2.0.x到2.1.2版本的核心过滤器.! StrutsPrepareAndExecuteFilter是自2.1.3开始就替代了FilterDispatcher的!

    --这样的改革当然是有好处的,为什么这么说? 应该知道如果我们自己定义过滤器的话, 是要放在strtus2的过滤器之前的, 如果放在struts2过滤器之后,你自己的过滤器对action的过滤作用就废了,不会有效!除非你是访问jsp/html!

    那我现在有需求, 我必须使用Action的环境,而又想在执行action之前拿filter做一些事, 用FilterDispatcher是做不到的!

    那么StrutsPrepareAndExecuteFilter可以把他拆分成StrutsPrepareFilter和StrutsExecuteFilter,可以在这两个过滤器之间加上我们自己的过滤器!

    --打个比喻, 现在有病人要做手术, 现在struts2要做两件事, 搭病床(环境),执行手术! 那么打麻药的工作呢.? 不可能要病人站着打吧, 所以必须有病床的环境,打完麻药之后再动手术! 这个比喻非常形象了!

    从Struts2.1.3开始,将废弃ActionContextCleanUp过滤器,而在StrutsPrepareAndExecuteFilter过滤器中包含相应的功能。

    三个初始化参数:

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

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

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

    <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>com.cjm.web.action</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

    最后,如果程序报类找不到异常,且后面有 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter等信息,可能是你的版本问题.解决方法是确定你的struts版本,如果是2.1.3之前的版本,用 org.apache.struts2.dispatcher.FilterDispatcher,以后的版本则配置org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter。

    struts2-流程详解-StrutsPrepareAndExecuteFilter

     

    http://hi.baidu.com/liangzhongbo/item/07be3a560c16bc13da163536

    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

    初始化:

    public void init(FilterConfig filterConfig)
    throws ServletException
    {
    InitOperations init = new InitOperations();
    FilterHostConfig config = new FilterHostConfig(filterConfig);
    init.initLogging(config);
    Dispatcher dispatcher = init.initDispatcher(config);
    init.initStaticContentLoader(config, dispatcher);
    prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
    execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
    excludedPatterns = init.buildExcludedPatternsList(dispatcher);
    postInit(dispatcher, filterConfig);
    init.cleanup();
    break MISSING_BLOCK_LABEL_108;
    Exception exception;
    exception;
    init.cleanup();
    throw exception;
    }

    执行任务

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)

    throws IOException, ServletException
    {
    HttpServletRequest request;
    HttpServletResponse response;
    request = (HttpServletRequest)req;
    response = (HttpServletResponse)res;
    prepare.setEncodingAndLocale(request, response);

    /*为当前线程创建ActionContext,ActionContext是ThreadLocal的,ActionContent其实就是一个线程安全的HashMap,它内部使用一个HashMap

    来储存相关信息,这个map的构造是使用dispatcher.createContextMap(request, response, null, servletContext)来形成的,这个

    map包括的信息有session,request,response,ServletContext,RequestMap,SessionMap等各种信息,可以通

    个这个ActionContent取得各种信息,例如ActionContext.getContext().getSession()来获得当前的Session,

    如果要获得request或者response必须用ServletActionContext它是ActionContent的子类,提供更多的方法,如

    果是forward那也会新建一个新的ActionContent,这个新的ActionContent直接使用旧的ActionContent的数据来

    构造*/

    prepare.createActionContext(request, response);
    prepare.assignDispatcherToThread();
    if(excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns))
    {
    chain.doFilter(request, response);
    } else
    {
    request = prepare.wrapRequest(request);
    org.apache.struts2.dispatcher.mapper.ActionMapping mapping = prepare.findActionMapping(request, response, true);
    if(mapping == null)
    {
    boolean handled = execute.executeStaticResourceRequest(request, response);
    if(!handled)
    chain.doFilter(request, response);
    } else
    {
    execute.executeAction(request, response, mapping);
    }
    }
    prepare.cleanupRequest(request);
    break MISSING_BLOCK_LABEL_178;
    Exception exception;
    exception;
    prepare.cleanupRequest(request);
    throw exception;
    }

  • 相关阅读:
    PAT (Advanced Level) 1060. Are They Equal (25)
    PAT (Advanced Level) 1059. Prime Factors (25)
    PAT (Advanced Level) 1058. A+B in Hogwarts (20)
    PAT (Advanced Level) 1057. Stack (30)
    PAT (Advanced Level) 1056. Mice and Rice (25)
    PAT (Advanced Level) 1055. The World's Richest (25)
    PAT (Advanced Level) 1054. The Dominant Color (20)
    PAT (Advanced Level) 1053. Path of Equal Weight (30)
    PAT (Advanced Level) 1052. Linked List Sorting (25)
    PAT (Advanced Level) 1051. Pop Sequence (25)
  • 原文地址:https://www.cnblogs.com/zimohul/p/6509073.html
Copyright © 2011-2022 走看看