Filters vs Interceptors in Struts 2

后端 未结 6 1731
后悔当初
后悔当初 2021-01-31 04:29

What\'s the difference, really, between filters and interceptors? I realize that interceptors fire before and after an action, recursively, and filters can be configured to fir

6条回答
  •  不知归路
    2021-01-31 05:11

    The most significant difference is that "interceptors" are a part of the Struts 2 framework, and are only part of the request handling that is done by the Struts 2 framework. "Filters" on the other hand are a part of the Servlet Specifcation; in other words, they are part of the Servlet API. If you are using Struts 2, you should use interceptors for wrapping functionality around your Struts 2 actions. If you are trying to wrap functionality around requests coming to your webapp, but not being handled by Struts 2, then a filter might be more appropriate.

    BTW, the entire Struts 2 Framework is deployed inside a filter configured in your web app, declared in your webapp's deployment descriptor ( web.xml ) like:

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

    This filter, which is configured to catch all requests URL patterns, is the entry point into the entire Struts 2 framework.

    I hope that helps.

提交回复
热议问题