[Original]java Web Learning Note 74:struts2 Learning path--custom blocker, struts built-in interceptor

Source: Internet
Author: User

The purpose of this blog: ① summary of their own learning process, equivalent to study notes ② to share their own experience to everyone, learn from each other, communication, not commercialcontent inevitably appear problems, welcome to correct, exchange, discussion, you can leave a message, can also be contacted by the following ways. I Internet technology enthusiasts, Internet technology enthusiastsWeibo: Ivan is in 0221qq:951226918

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -----------------------------

1.struts2 Interceptor

1) Interceptors (Interceptor) are a core component of Struts 2.

2) Struts2 Many of the functions are built on interceptors, such as file upload and download, internationalization, data type conversion and data validation, and so on.

3) Struts2 interceptors to intercept before or after accessing an Action method

4) Struts2 interceptors are pluggable, and interceptors are an implementation of AOP (aspect-oriented programming).

5) 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 model of 2.STRUT2

3.Struts2 's own interceptor

  

  

4.Interceptor interface

1) Each interceptor is a Java class that implements the interface:

    

    -Init: This method is called immediately after the Interceptor is created, and it is called only once during the life cycle of the interceptor. The necessary initialization of related resources can be made in this method

-Interecept: This method will be called once per request is intercepted.

- Destroy: This method is called before the Interceptor is destroyed, and it is called only once during the life cycle of the interceptor.

2) Struts invokes the Interecept method of each interceptor registered for an Action in turn.

3) each time the Interecept method is called, Struts passes an instance of the actioninvocation interface.

4)Actioninvocation: Represents the execution state of a given action, and the interceptor can get the action object and the Result object associated with the action from the object of the class. After completing the interceptor's own task, the interceptor advances to the next link in the Action process by invoking the Invoke method of the Actioninvocation object.

5) abstractinterceptor : class implements the Interceptor interface. And for Init, destroy provides a blank implementation

5. Custom Interceptors

1) Define the class of an interceptor

> can implement Interceptor interface

> Inheritance Abstractinterceptor abstract class

2) in the Struts.xml file configuration

Myintercepter.java

1  PackageCom.jason.intercepter;2 3 Importorg.eclipse.jdt.internal.compiler.ast.Invocation;4 5 Importcom.opensymphony.xwork2.ActionInvocation;6 ImportCom.opensymphony.xwork2.interceptor.AbstractInterceptor;7 8  Public classMyintercepterextendsabstractinterceptor{9 Ten     /** One * @Fields: Serialversionuid A      */ -     Private Static Final LongSerialversionuid = 1L; -  the @Override -      PublicString Intercept (actioninvocation invocation)throwsException { -System.out.println ("Before Invovation.invoke ... "); -String result =Invocation.invoke (); +System.out.println ("After Invovation.invoke ... "); -          +         returnresult; A     } at  -}

Struts.xml Configuring custom interceptors and using interceptors

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "Http://struts.apache.or G/dtds/struts-2.3.dtd "><Struts>< Packagename= "Default"namespace="/"extends= "Struts-default"><Interceptors>                    <Interceptorname= "Hello"class= "Com.jason.intercepter.MyIntercepter"></Interceptor></Interceptors><Actionname= "Testtoken"class= "Com.jason.upload.app.TokenAction">             <!--<interceptor-ref name= "tokensession" ></interceptor-ref> -                         <interceptor-ref  name= "Hello"></interceptor-ref  >                             <Interceptor-refname= "token"></Interceptor-ref>             <Interceptor-refname= "Defaultstack"></Interceptor-ref>             <result>/success.jsp</result>             <resultname= "Invalid.token">/token-error.jsp</result>         </Action></ Package></ Package>

3) Note:

The Invoke () method without actioninvocation can be selected in a custom interceptor. Then the interceptor and action methods are not called. Struts renders custom interceptors intercept method return value corresponding to result

 

[Original]java Web Learning Note 74:struts2 Learning path--custom blocker, struts built-in interceptor

Related Article

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.