The difference and execution order of interceptor and filter in Struts2

Struts2 architecture diagram:

when a httprequest is received,
a) when an external httpservletrequest arrives
b) initially to the servlet container and passed to a standard filter chain
c) FilterDispatcher will look for the corresponding ActionMapper, if it finds the corresponding ActionMapper it Will give control to ActionProxy
d) ActionProxy will look up configuration struts.xml through ConfigurationManager
       i. Next step will be responsible for the implementation of command mode through ActionInvocation (including calling some interceptor framework before calling action)
       ii. Interceptor Do some interception or initial work
e) Once the action returns, it will look for the corresponding Result
f) The Result type can be jsp or freeMark, etc.
g) These components and ActionMapper are returned to the requested url (note the execution order of the interceptor)
h) The response is returned through the filter we configured in web.xml
i) If ActionContextCleanUp is currently used, FilterDispatcher will not clean up the sreadlocal ActionContext; if ActionContextCleanUp is not used, it will clean up sreadlocals.





1. Interceptors are based on the java reflection mechanism, while filters are based on function callbacks.
2. The filter depends on the servlet container, while the interceptor does not depend on the servlet container.
3. Interceptors can only work on Action requests, while filters can work on almost all requests.
4. Interceptors can access objects in the Action context and value stack, while filters cannot.
5. In the life cycle of Action, the interceptor can be called multiple times, while the filter can only be called once when the container is initialized.





Four functions



of org.apache.struts2.dispatcher.FilterDispatcher org.apache.struts2.dispatcher.FilterDispatcher is the main Filter of Struts2, responsible for four functions:
      (1) Execute Actions
        (2) Clear ActionContext
        (3) Maintain Static content
        (4) Clear the XWork interceptors in the request life cycle.
               Another note: this filter should filter all request URLs. Generally set to /*. (filter all requests!)
    Specifically:
        (1) Execute the Actions
            filter through the ActionMapper object to determine whether it should be mapped to an Action. If the mapper object indicates that it should be mapped, the filter chain will be Terminate, then Action is called. This is very important, if the SiteMesh filter is used at the same time, the SiteMesh filter should be placed in front of the filter, otherwise the output of the Action will not be decorated.
        (2) Clear the ActionContext
            filter In order to ensure memory overflow, the ActionContext will be automatically cleared. This can be problematic when integrating with other frameworks such as SiteMesh. ActionContextCleanUp provides some information on how to deal with these problems.
        (3) Maintaining static content The
            filter will also maintain some public static content used in Struts2, such as JavaScript files, CSS files, etc. Search for requests in the /struts/* scope, then map the value after /struts/ to some common struts package, or in your classpath. By default it will look for the following package: org.apache.struts2.static.template. This way you just request /struts/xhtml/styles.css and the default stylesheet for the XHTML UI theme will be returned. Likewise, the JavaScript files required by AJAX UI components can also be found in the org.apache.struts2.static package. If you want to include other packages that are searched, set the filter in web.xml by giving the "actionPackages" initial parameter a comma-separated list of packages values.
            It should be noted that it will expose some more sensitive information such as database connection information in the properties file.
    Note: The filter supports the following initial parameters:
         config - A comma-separated list of XML files to be called.
         actionPackages - A comma-separated list of packages to be scanned by actions.
         configProviders - Comma separated implementation classes that implement the ConfigurationProvider interface (used when building the Configuration).
         * - an arbitrary struts constant.
    Dispather can be customized by overriding the createDispatcher() method.
   
    Attribute list:
         (1) actionMapper: Provide an ActionMapper instance through injection.
         (2) dispatcher: expose a Dispatcher instance to subclasses.
         (3) encoding: Stores the settings of StrutsConstants.STRUTS_I18N_ENCODING.
         (4) filterConfig: Provide a FilterConfig instance through the initial parameter.
         (5) lastModifiedCal: In caching static content, a formatted date is provided for setting header information.
         (6) log: provide a logging instance.
         (7) patchPrefixs: path prefix information for storing static resources.
         (8) serveStatic: Stores the settings of StrutsConstants.STRUTS_SERVE_STATIC_CONTENT.
         (9) serveStaticBrowserCache: Stores the settings of StrutsConstants.STRUTS_SERVE_STATIC_BROWSER_CACHE.
    List of methods:
         (1) copy(InputStream input, OutputStream output): Copy data from input to output.
         (2) createDispatcher(FilterConfig filterConfig): Create a default dispatcher object. If necessary, subclasses can override this method to customize a dispatcher object.
         (3) Destory(): Call dispatcher.cleanup(), release the local thread in turn, and destroy the dispatcher object.
         (4) doFilter(ServletRequest request, ServeltResponse response, FilterChain chain): Process an action or process the static content of a request.
         (5) findInputStream(String name, String packagePrefix): Search for static resources under the classpath.
         (6) findStaticResoruce(String name, HttpServletRequest request, HttpServletResponse response): Search for static resources and copy them directly into the header information of the corresponding response.
         (7) getContentType(String name): Get the contentType of the specified resource.
         (8) getFilterConfig(): Get an instance of FilterConfig.
         (9) getServletContext(): Provides a workspace for some versions of WebLogic.

        (9) init(FilterConfig filterCongfig): Create a default dispatcher object and set the default package information of static resources to initialize the filter.
         (10) parse(String packages): Returns an array by parsing a specified comma-separated list of packages.
         (11) prepareDispatcherAndWrapRequest(HttpServletRequest request, HttpServletResponse response): Encapsulate the given request object and return an encapsulated HttpServletRequest object. For example shown handles multipart data.
         (12)setMapper(ActionMapper actionMapper)
         (13)setEncoding(String val)
         (14)setServeStaticContent(String val)
         (15)setServeStaticBrowserCache(String val)




Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326945644&siteId=291194637