Why use interceptors?
Struts is a development framework based on the MVC pattern. Any framework, a subset of the functionality will be implemented in advance. The implementation of the self-feature of the struts framework is accomplished by a interceptor.
Struts early in the release, the core functionality is done through a class called the core filter.
After the Struts2.0 version, each interception function is implemented by a separate class, creating a standalone interceptor that allows users to freely assemble these interceptors to perform their desired functions.
With interceptors, our development is similar to assembling computer accessories. Benefits: Decoupling.
Interceptor Introduction:
STRUTS2 has predefined interceptors that implement certain functions, such as file uploads, and the default interceptors are configured in the Struts-default.xml file.
If you use the corresponding function of struts2, you can refer to the interceptor! You can also define the interceptor stack, which contains multiple interceptors! You can use the interceptor stack directly in development!
STRUTS2 defines the default interceptor stack to execute!
The interceptor can intercept the action request to perform some preprocessing operations, and after the preprocessing operation is complete, control enters the action!
Action processing request completed, Back to interceptor!
Attention:
Users can also customize interceptors if struts comes with an interceptor that does not complete the need!
If the user has customized the interceptor and uses it, the STRUTS2 default interceptor will not be invoked!
When customizing the Interceptor, refer to the default interceptor! (Defaultstack)
<interceptors>
Interceptor definition
<interceptor name= "params" class= "Com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
Interceptor Stack (Interceptor stack): The Interceptor is connected in a certain order into a chain. When accessing the intercepted method, interceptors in the Struts2 interceptor chain are called sequentially in the order in which they were previously defined.
Interceptor stack definition (contains multiple interceptors)
<interceptor-stack name= "Defaultstack" >
<interceptor-ref name= "Exception"/>
<interceptor-ref name= "Alias"/>
<interceptor-ref name= "ServletConfig"/>
<interceptor-ref name= "i18n"/>
<interceptor-ref name= "Prepare"/>
<interceptor-ref name= "Chain"/>
<interceptor-ref name= "Scopedmodeldriven"/>
<interceptor-ref name= "Modeldriven"/>
<interceptor-ref name= "FileUpload"/>
<interceptor-ref name= "checkbox"/>
<interceptor-ref name= "MultiSelect"/>
<interceptor-ref name= "Staticparams"/>
<interceptor-ref name= "Actionmappingparams"/>
<interceptor-ref name= "params" >
</interceptor>
Execute Interceptor
<default-interceptor-ref name= "Defaultstack"/>
</interceptors>
Interceptors differ from filters
Common denominator: All requests can be intercepted! Handle some of the common operations!
Difference:
1. Scope of Action
Interceptors are the concept of struts2! (in struts only)
The filter is the servlet technology! (Can be used in servlet/struts!) )
2. Intercept area
Interceptor, which intercepts the execution of the method in action!
Filter, can intercept any request!
STRUTS2 Custom Interceptor Implementation
1, define a class implementation interceptor interface
2. Implementing the Life cycle approach
3. Declaring and referencing a custom interceptor
Interceptor interface
Com.opensymphony.xwork2.interceptor.Interceptor
All custom interceptors need to implement this interface and run in a single case.
Init: Called after the creation of the initialization, it is called only once during the life cycle of the interceptor.
Interecept: Every action request is intercepted and the method is called once.
Destroy: The interceptor is called before it is destroyed, and it is only called once during the life cycle of the interceptor.
Struts2 comes with an interceptor
Struts Framework Learning Interceptor