Simple understanding of the difference between interceptor and filter in STRUTS2 and the order of execution

Source: Internet
Author: User
Tags file upload i18n prepare


When a HttpRequest is received,
A) when the external HttpServletRequest arrives
b) initially to the servlet container passed to a standard filter chain
c) Filterdispatecher will go to find the corresponding actionmapper, if found the corresponding actionmapper it will give control authority to Actionproxy
d) Actionproxy will find the configuration via ConfigurationManager Struts.xml
I. The next step will be to actioninvocation the implementation of the command pattern (including invoking some blocking interceptor framework before calling the action)
Ii. Interceptor to do some interception or initial work
e) Once the action returns, the corresponding result is found
f) The result type can be JSP or Freemark, etc.
G These components are returned with the Actionmapper to the requested URL (note the order in which the Interceptor is executed)
h) The response is returned through the filters we configured in the Web.xml
i) if the actioncontextcleanup is currently in use, Filterdispatecher will not clean the sreadlocal actioncontext, if Actioncontextcleanup is not used, will be to clean up the sreadlocals.


1, the Interceptor is based on the Java reflection mechanism, and the filter is based on function callback.
2, the filter relies on the servlet container, and the interceptor does not depend on the servlet container.
3. The interceptor can only function on the action request, and the filter works on almost any request.
4, the interceptor can access the action context, value stack objects, and filters can not.
5. In the life cycle of the action, the interceptor can be invoked multiple times, and the filter can only be invoked once when the container is initialized.


Four features of Org.apache.struts2.dispatcher.FilterDispatcher

Org.apache.struts2.dispatcher.FilterDispatcher is the main filter of STRUTS2, responsible for four aspects of the function:
(1) Enforcement actions
(2) Clear Actioncontext
(3) Maintain static content
(4) Clear the interceptors of the xwork in the request life cycle

Note: The filter should filter all request URLs. is generally set to/*. (all requests are filtered.) )

Specific:
(1) Enforcement actions
Filters are Actionmapper objects to determine whether they should be mapped to action. If the Mapper object indicates that he should be mapped, the filter chain is terminated and the action is invoked. This is important if you use Sitemesh filter at the same time, Sitemesh filter should be placed before the filter, otherwise the action output will not be decorated.
(2) Clear Actioncontext
The filter automatically clears the Actioncontext to ensure memory overflow. This can be problematic when integrated with other frameworks, such as Sitemesh. Actioncontextcleanup provides some information on how to handle these issues.
(3) Maintain static content
Filters also maintain some of the public static content used in Struts2, such as JavaScript files, CSS files, and so on. Search for/struts/*-scoped requests, and then map the values that follow/struts/to some of the public packages of struts, or you can search in your classpath. By default, the following packages are looked up: org.apache.struts2.static.template. So you only use the request/struts/xhtml/styles.css,xhtml the UI theme The default stylesheet will be returned. Similarly, JavaScript files required by AJAX UI components can also be found in the Org.apache.struts2.static package. If you want to join other searched packages, set the filter in Web.xml by giving the "actionpackages" initial parameter a comma-separated packet list value.
Note that he exposes some of the more sensitive information, such as database connection information in the Properites file.
Note: The filter supports the following initial parameters:
Config-a comma-delimited list of XML files to be transferred.
Actionpackages-A comma-separated list of packages that is scanned by the actions.
Configproviders-comma-delimited implementation classes that implement the Configurationprovider interface (used when building configuration).
*-any struts constant.

You can customize Dispather by overloading the Createdispatcher () method.
    
Property list:
         (1) Actionmapper: by injecting, Provides a actionmapper instance.
         (2) Dispatcher: Exposing to a dispatcher instance of a subclass.
         (3) Encoding: storage strutsconstants.struts_i18n_encoding settings.
         (4) Filterconfig: Provides a filterconfig instance from the initial parameter.
         (5) Lastmodifiedcal: In cache static content, provides a grid date for setting header information.
         (6) Log: Provides a logging instance.
         (7) Patchprefixs: Store path prefix information for a static resource.
         (8) servestatic: Storage strutsconstants.struts_serve_static_ The setting of the content.
         (9) Servestaticbrowsercache: Storage strutsconstants.struts_serve_ The setting of the Static_browser_cache.

Method List:
(1) Copy (InputStream input, outputstream output): Copy data from input to output.
(2) Createdispatcher (Filterconfig filterconfig): Creates a default Dispatcher object, which, if necessary, can be overloaded by subclasses, customizing a Dispatcher object.
(3) Destory (): Invokes Dispatcher.cleanup (), releases the local thread in turn, and destroys the Dispatcher object.
(4) Dofilter (ServletRequest request, serveltresponse response, Filterchain chain): Handles an action or handles a request's static content.
(5) Findinputstream (string name, String packageprefix): Searches for static resources under the CLASSPATH.
(6) Findstaticresoruce (String name, httpservletrequest request, httpservletresponse response): Searching for static resources, Copy directly to the corresponding response header information.
(7) getContentType (String name): Gets the contenttype of the specified resource.
(8) Getfilterconfig (): Gets Filterconfig instance.
(9) Getservletcontext (): Provides a workspace for some versions of WebLogic.

(a) init (Filterconfig filtercongfig): Creates a default dispatcher object and sets the default package information for static resources to initialize filter.
(one) parse (String packages): Returns an array that resolves a comma-delimited list of packages.
(a) Preparedispatcherandwraprequest (HttpServletRequest request, httpservletresponse response) : encapsulates the given request object to return a encapsulated HttpServletRequest object. For example, the processing multipart data displayed.
(Setmapper) (Actionmapper actionmapper)
(setencoding) (String val)
(a) setservestaticcontent (String val)
(Setservestaticbrowsercache) (String val)

Report:

STRUTS2 (xwork) provides a description of the function of the Interceptor:

Intercepting device

Name

Description

Alias Interceptor

Alias

Request parameters are converted between different requests in different name parts, and the request content is unchanged

Chaining Interceptor

Chain

The previous Action property can be accessed by the latter action and is now used in conjunction with the chain type of result (<result type= "Chain" >).

Checkbox Interceptor

CheckBox

Added checkbox automatic Processing code to set the contents of unselected checkbox to False, and HTML does not commit unchecked checkbox by default.

Cookies Interceptor

Cookies

Use of Name,value to refer to cookies

Conversion Error Interceptor

Conversionerror

Adds an error from Actioncontext to the property field of the action.

Create Session Interceptor

CreateSession

Automatic creation of HttpSession to serve interceptors that require the use of httpsession.

Debugging Interceptor

Debugging

Provide different debugging pages to show the internal data situation.

Execute and Wait Interceptor

Execandwait

Performs an action in the background while bringing the user to a waiting page in the middle.

Exception Interceptor

exception

To position an exception to a screen

File Upload Interceptor

FileUpload

Provide file Upload function

I18N Interceptor

i18n

Record User-selected locale

Logger Interceptor

Logger

Output the name of the action

Message Store Interceptor

Store

Stores or accesses messages, errors, field errors, etc. that occur with the action class that implements the Validationaware interface.

Model Driven Interceptor

Model-driven

If a class implements Modeldriven, the result of the Getmodel is placed in the value stack.

Scoped Model Driven

Scoped-model-driven

If an action implements the Scopedmodeldriven, the Interceptor takes the Setmodel method of the model call action from the corresponding scope and puts it inside the action.

Parameters Interceptor

Params

Sets the parameters in the request to the action.

Prepare Interceptor

Prepare

If Acton implements Preparable, the interceptor invokes the prepare method of the action class.

Scope Interceptor

Scope

An easy way to save the action state in session and application.

Servlet Config Interceptor

ServletConfig

Provides a way to access HttpServletRequest and HttpServletResponse, which is accessed as a map.

Static Parameters Interceptor

Staticparams

From the Struts.xml file, set the contents of <param> in <action> to the corresponding action.

Roles Interceptor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.