SPRINGMVC's Interceptor

Source: Internet
Author: User

The implementation of the Handlerinterceptor interface is used in this article

1. Custom Interceptors:

/*** interception Device*/ Public classCustomerinterceptorImplementshandlerinterceptor{/*** The Handlerinterceptor interface defines three methods * Execution order: * 1.preHandle: * When the request comes in, it takes precedence to enter Prehandle This method if return true request Release, if return false request immediate termination * 2. Execute the controller to intercept * 3.postHandle: * When the Prehandle method is put back to true and the controller executes after completion will enter POS Thandle This method * 4.afterCompletion: * When Posthandle execution is complete, execute the Aftercompletion method*/@Override Public BooleanPrehandle (httpservletrequest request, httpservletresponse response, Object handler)throwsException {System.out.println ("----start intercepting----."); return true; } @Override Public voidPosthandle (httpservletrequest request, httpservletresponse response, Object handler, Modelandvi EW Modelandview)throwsException {if(modelandview!=NULL) {Modelandview.addobject ("Date",NewDate ()); } System.out.println ("------1-----after interception"); } @Override Public voidaftercompletion (httpservletrequest request, httpservletresponse response, Object handler, Exception ex) throwsException {System.out.println ("------2-----after interception"); }}

The following configuration is added to the 2.SPRINGMVC configuration file:

<mvc:interceptor> can configure multiple
When configuring multiple, according to <mvc:interceptor> configuration order, first the first interceptor Prehandle, and so on after all the Prehandle executed,
In accordance with the configuration order of <mvc:interceptor>, the posthandle of the last interceptor is executed first.
<!--Interceptor Configuration -    <BeanID= "Customerinterceptor"class= "Zpark.interceptor.CustomerInterceptor"/>        <!--to add interceptors to the SPRINGMVC interception system. -    <mvc:interceptors>        <Mvc:interceptor>             <!--the path to intercept -            <mvc:mappingPath= "/json/**"/>            <!--referencing a custom interceptor -            <refBean= "Customerinterceptor"/>        </Mvc:interceptor>    </mvc:interceptors>

SPRINGMVC'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.