Struts2 learning-interceptor

Source: Internet
Author: User

Directory

1. Struts2 Interceptor:

1) Use of struts2 interceptor

2) struts built-in interceptor

3) interceptor usage

Ii. How the interceptor Works)

1. Struts2 Interceptor: 1) Use of struts2 Interceptor

1. the Interceptor is the extended Action Mechanism of struts2. The Interceptor is a class that implements the Interceptor interface. Some system-level general business logic is implemented through the interceptor. Such as logging, permission control, and transaction control

2. Use Interceptor

A) create an Interceptor component to implement the Interceptor interface.

B) register the interceptor in struts. xml.

C) In struts. xml, specify the interceptor for the action.

D) if the custom Interceptor is referenced in the Action, the default Interceptor will not be executed.

3. Usage

<Struts> <package name = "demo" namespace = "/demo" extends = "struts-default"> <! -- Define an interceptor group --> <interceptorsname = "myInterceptors"> <! -- Define your own interceptor --> <interceptor name = "myInterceptor" class = "com.tar ena. interceptor. MyInterceptor"> </interceptor> </interceptors> <! -- Defines the interceptor stack, which can be referenced by Action as a common interceptor --> <interceptor-stackname = "myInterceptorStack"> <! -- Reference interceptor --> <interceptor-refname = "myInterceptor"> </interceptor-ref> </interceptor-stack> <actionname = "myAction" class = "com.tar ena. action. myAction "> <! -- Reference interceptor --> <intercepter-refname = "myInterceptorStack"> </interceptor-ref> </action> </package> </struts>

4. the interceptor in Struts has a similar function as the Filter in Servlet. the interceptor in Struts also implements the same function. You can perform some operations before or after a specific Action is called, configuration-based management is easy to use.

650) this. width = 650; "src =" http://img1.51cto.com/attachment/201309/144133709.jpg "title =" 485262859850344790.jpg"/>

2) struts built-in interceptor

Alias: converts aliases of similar parameters in different requests.

Autowiring: This is an automatically assembled Interceptor. It is mainly used to access beans in Spring containers when Struts2 and Spring are integrated.

Chain: Build an Action chain so that the current Action can access the attributes of the previous Action, which is generally used together with <resulttype = "chain".../>.

ConversionError: This is an interceptor responsible for handling type conversion errors. It is responsible for extracting type conversion errors from ActionContext and converting them to FieldError errors of Action.

CreateSession: This interceptor creates an HttpSession object, which is mainly used in the interceptor that requires an HttpSession object to work normally.

Debugging: When the development mode of Struts2 is used, this interceptor provides more debugging information.

ExecAndWait: the background executes the Action to send the waiting image to the user.

Exception: This interceptor is responsible for handling exceptions and maps exceptions to results.

FileUpload: This interceptor is mainly used for file upload. It is used to parse the content of the file fields in the form.

I18n: This is an interceptor that supports internationalization. It is responsible for putting the selected language and region into the user Session.

Logger: This is an interceptor responsible for logging, mainly the name of the output Action.

Model-driven: this is a model-driven Interceptor. When an Action class implements the ModelDriven interface, it is responsible for heap the results of the getModel () method into ValueStack.

Scoped-model-driven: if an Action implements a ScopedModelDriven interface, the interceptor is responsible for finding the specified Modol from the specified survival range and passing the Model to the Action instance through the setModel method.

Params: This is the most basic Interceptor. It is responsible for parsing parameters in the HTTP request and setting the parameter value to the attribute value corresponding to the Action.

Prepare: If action implements the Preparable interface, the prepare () method of the interceptor is called.

Static-params: This interceptor is used to pass parameters in the <action> label <param> label in xml into the action.

Scope: This is the range conversion Interceptor. It can save the Action status information to the range of HttpSession or to the range of ServletContext.

Servlet-config: if an Action needs to directly access the Servlet API, it is implemented through this interceptor.

Note: Avoid directly accessing the Servlet API in Action as much as possible, which will lead to high coupling between Action and Servlet.

Roles: This is a JAASJava Authentication andAuthorization Service, Java authorization and Authentication Service) interceptor. Only when the browser obtains the appropriate authorization can it call the Action intercepted by the interceptor.

Timer: This interceptor is responsible for outputting the execution time of the Action. This interceptor is useful in analyzing the performance bottleneck of the Action.

Token: This interceptor is mainly used to prevent repeated submissions. It checks the tokens uploaded to the Action to prevent multiple submissions.

Token-session: This interceptor serves basically the same purpose as the previous one, but stores the token in HttpSession.

Validation: Performs data validation by executing the validator defined in the xxxAction-validation.xml.

Workflow: This interceptor is responsible for calling the validate method in the Action class. If verification fails, the logic view of input is returned.

Most of the time, developers do not need to manually control these interceptors, because these interceptors have been configured in the struts-default.xml file, as long as the packages we define inherit the struts-default package of the system, you can directly use these interceptors.

3) interceptor usage

Inherits AbstractInterceptor and overwrites the interceptexceptioninvocationinvocation method.

Inherit MethodFilterInterceptor, override dointerceptexceptioninvocationinvocation) When configuring parameters, you can specify filtering methods or excluded methods.

Public class FirstInterceptor implements Interceptor {public void destroy () {} public void init () {} public String intercept (ActionInvocation ai) throws Exception {// before calling Action, the executed code System. out. println ("before call: FirstInterceptor. intercept ()... "); // call the corresponding Action String result = ai. invoke (); // The code System that is executed after the Action is called. out. println ("after the call: FirstInterceptor. intercept ()... "); return" error ";}}

Struts. xml

<Package name = "demo" namespace = "/demo" extends = "struts-default"> <! -- A small example of Struts2 interceptor --> <interceptors> <! -- Custom interceptor --> <interceptor name = "firstInterceptor" class = "com.tar ena. interceptor. firstInterceptor "> </interceptor> <interceptor name =" secondInterceptor "class =" com.tar ena. interceptor. secondInterceptor "> </interceptor> <! -- Custom interceptor stack, interceptor packaging --> <interceptor-stack name = "iner"> <interceptor-ref name = "firstInterceptor"> </interceptor-ref> <interceptor-ref name = "secondInterceptor"> </interceptor-ref> </interceptor-stack> </interceptors> <action name = "some" class = "com.tar ena. action. someAction "> <! -- <Interceptor-ref name = "firstInterceptor"> </interceptor-ref> --> <! -- <Interceptor-ref name = "secondInterceptor"> </interceptor-ref> --> <! -- It is difficult to use interceptors one by one. You can use the interceptor stack as a group of interceptors) --> <interceptor-ref name = "iner"> </interceptor-ref> <result name = "success">/WEB-INF/OK. jsp </result> <result name = "error">/WEB-INF/error. jsp </result> </action> </package>

Action:

public String execute() {        System.out.println("SomeAction.execute()...");        return "success";    }


This article from the "beautiful life needs to carefully record" blog, please be sure to keep this http://huing.blog.51cto.com/1669631/1286616

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.