Struts2 Interceptor Theory Knowledge

Source: Internet
Author: User
Tags file upload http request

Software design dry rules, that is, do not write repetitive code, which can greatly reduce the workload of late maintenance. But the problem is, duplicate code is unavoidable, such as the user before sending a request to determine whether the user login, how to do it. The duplicate code is then extracted and encapsulated into a method A, which avoids writing duplicate code, and the user simply calls method a before sending the request. But again, the problem arises, the user may call method A before sending the request, may call method B, because it was hard-coded before the method a written in the judgment of whether the user is logged in, and then have to change the code before each request, it is troublesome, how to do it. We need a mechanism to solve hard coding problems and actually implement method invocations. So the Struts2 interceptor appeared to fulfill this need.
What is an interceptor. Interceptors are a class from a code perspective, and the class contains methods that are automatically executed by the system driver before the target method call. Why is it called an interceptor? Because what this class does, the resulting behavior is an interception, so call it an interceptor.
Interceptor principle: The Interceptor method is invoked by means of a proxy. In general, the system takes the target object as the blueprint, creates a proxy object, and the method in the proxy object is a combination of the Interceptor method and the target object method. Therefore, the Interceptor method is executed before and after the target method is executed.
STRUTS2 Interceptor Strategy: Each interceptor completes a function, which interceptor needs to be used, in the Struts.xml to specify the use of interceptors on the line, so that the Interceptor method before the target method execution or after execution of the automatic execution, to complete the dynamic insertion of common operations. You can also put multiple interceptors together to form a more powerful interceptor stack.
Struts2 the requested process in the framework: STRUTS2 is an empty container with a large number of built-in interceptors complete most of the framework's operations, so it is possible to conclude that most of the STRUTS2 framework functions are implemented by interceptors when Filterdispatcher (configured in Web. xml) After intercepting the user request, a large number of interceptors process the user request, then call the method in the action instance to process the request, return to the logical view, and present the resource corresponding to the logical view to the user.
A large number of interceptors are built into the STRUTS2, which are configured in NAME-CALSS pairs in the Struts-default.xml file (the default configuration file for Struts2, which is automatically loaded). When the package we defined in the Struts.xml file inherits the STRUTS2 default Struts-default package (which specifies the Defalutstack interceptor stack as the default interceptor stack, if no interceptor reference is specified in the action configuration, the system will Defalutsta CK Interceptor Stack automatically acts on the action), you can freely use the struts2 built-in interceptors. If you do not inherit, we need to customize, the difficulty increases immediately, so it is generally necessary to inherit.
What are the built-in interceptors?
Alias: Implements the conversion of similar parameter aliases in different requests.
Autowiring: Automatic assembly for access to bean! in spring containers
Chain: Constructs a antion chain that accesses the property 2 of the previous action using the current acition.
Conversionerror: The Interceptor responsible for handling type conversion errors.
Cookies: Allows multiple cookie! to be added to the action in a configured manner
CreateSession: Responsible for creating a HttpSession object.
Debugging: provides more debugging information.
Execandwait: Send the wait screen to the user.
Exception: Handles the exception and maps the exception to the result.
FileUpload: Mainly used for file upload.
I18N: Support for international interceptors.
Logger: The interceptor responsible for logging.
Modeldriven: Used for model-driven interceptors.
Scopemodeldriven: Finds the specified model from the specified survival range and passes the model to the action instance through the Setmodel () method.
Params: is responsible for parsing the parameters in the HTTP request and setting the parameter value to the property value corresponding to the action.
Staticparam: Passes the parameters in the Param tag under the action tag in the XML to the action
Scope: Range Converter
ServletConfig: Direct access to servletapi in action.
Timer: Outputs the execution time of the action.
Token: Prevents duplicate commits.
Tokensession: Save the token in the httpsession.
Validation: Complete data validation.
Workflow: Call the Validate method in the action class, the checksum fails, and the input logical view is returned.
Store: Saves the information to the session.
CheckBox: Detects a check box that is not checked.
Roles: Authorization blocker
Annotationworkflow: An interceptor that supports the STRUTS2 "0 Configuration" feature.
MultiSelect: Check the drop-down list that is not selected.

How to use interceptors in struts2.
1. Define interceptors to define interceptors under package packages using the Interceptros element
2. Using interceptors
Method One: Reference the Interceptor in the action element.
Method Two: Configure the default interceptor in the package element to implement all the actions under the packet to configure the same interceptor at the same time.
The configuration elements associated with interceptors are as follows:
Interceptros defining interceptors This element contains interceptor and Interceptor-stack
Interceptor defining a single interceptor definition requires specifying name and class two properties
Interceptor-stack Defining the Interceptor Stack This element contains interceptor-ref child elements
Interceptor-ref reference an interceptor or interceptor stack
param used to specify parameters for interceptors
Default-interceptor-ref Configuring the default interceptor for the specified package

If an interceptor is applied to the action, the default interceptor (the default interceptor for STRUTS2 is Defaultstack) does not work, and if the action is to use the default interceptor, the interceptor reference must be manually configured, and if the specified interceptor is not displayed in the action, The default interceptor will work.

To develop your own interceptor steps:
1. Implementing the Interceptor Class
1.1 Implementing the Interceptor Interface
1.2 Inheriting the Abstractinterceptor class (Implementing the Interceptor method) intercepts all methods within the action
1.3 Inheriting Methodfilterinterceptor (is a subclass of Abstractinterceptor) intercepts the specified method within the action

Order of execution of interceptors:
The action before the Execute method, which is configured in front of the interceptor first, and for the action after the Execute method, the interceptor that is configured on the back will work first.

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.