Struts2 's Interceptor

Source: Internet
Author: User

The role of interceptors:

The interceptor can dynamically intercept requests sent to the specified action, and through the interceptor mechanism, you can insert some code before and after the action execution to implant our logical thinking.

The implementation principle of interceptors:

Interceptors are invoked by proxy, by dynamically acting on our action, by inserting our interception code before and after the action is executed. The idea of this interceptor is an AOP that gets the facets of the business process, on a particular plane

Specific methods are automatically inserted through the system.

                

Interceptors in the STRUTS2:

In an ordinary project, we often need to parse the file fields in the upload form to prevent the form from being submitted multiple times ..., these actions are not actions that all action needs to implement and need to be freely combined in a dynamic manner. The Interceptor strategy is

Good solution to this problem, the Interceptor method is executed automatically before or after the target method executes, which completes the dynamic insertion of the common operation.

            

Interceptor Configuration:

Define an Interceptor:

class = "Interceptor Implementation class" >        <param name= "parameter name" > Parameter value </param></interceptor>

You can also define an interceptor stack, which contains multiple interceptors.

<interceptor-stack name= "Interceptor stack name" >    <interceptor-ref name= "Interceptor One"/> <interceptor-ref    name= " Interceptor Two "/></interceptor-stack>

Custom interceptors:

To develop your own interceptor class, let's start by looking at how interceptor is.

 Public Interface extends Serializable {  void  destroy ();   void init ();   throws Exception;}

As you can tell from the above code, the Init method initializes the class, and the Destroy method is to destroy the class, and the Intercept method is your actual interception. In addition, STRUTS2 provides us with a abstractinterceptor class that we can use to reduce the amount of code by inheriting this class, starting with the implementation of our own interceptors.

@SuppressWarnings ("Serial") Public classShowtimeinterceptorextendsAbstractinterceptor {PrivateString name; @Override PublicString Intercept (actioninvocation actioninvocation)throwsException {System.out.println ("I am" +name); SYSTEM.OUT.PRINTLN (Name+ " : " +NewDate ()); String result=Actioninvocation.invoke (); SYSTEM.OUT.PRINTLN (Name+ " : " +NewDate ()); returnresult; }         PublicString GetName () {returnname; }         Public voidsetName (String name) { This. Name =name; }}

then configure the information in the Struts.xml:

<Interceptors>            <Interceptorname= "ShowTime"class= "Com.xujianguo.action.interceptor.ShowTimeInterceptor">                <paramname= "Name">Showtimeinterceptor</param>            </Interceptor>
</Interceptors><Actionname= "Login"class= "Com.xujianguo.action.LoginAction"> <resultname= "Success">/login-success.html</result> <resultname= "Error">/login.html</result>
<!--default Interceptor Defaultstack--<Interceptor-refname= "Defaultstack"></Interceptor-ref> <Interceptor-refname= "ShowTime"></Interceptor-ref></Action>

Struts2 's 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.