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