SPRINGMVC Study Notes (ix) custom interceptors

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " >spring mvc</span><span style= "font-family: Song body; Background-color:rgb (255, 255, 255); " > can also use interceptors to intercept requests, users can customize interceptors to implement specific functions, and custom interceptors must implement </span><span style= "Font-family:arial, Helvetica, Sans-serif; Color:rgb (255, 0, 0); Background-color:rgb (255, 255, 255); " >handlerinterceptor</span><span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > Interfaces </span>

① Prehandle (): This method is called before the business processor processes the request, in which the user requests the request for processing. Returns true if the programmer decides that the interceptor will call another interceptor after intercepting the request, or the business processor is going to process it, andif the programmer decides that no other components need to be called to process the request, the False.

② Posthandle (): This method is called after the business processor finishes processing the request, but before the dispatcherservlet returns a response to the client, the request is requested in the method be processed.

③ aftercompletion (): This method is called after the Dispatcherservlet has completely finished processing the request, and some cleanup of resources can be done in the method.

1.1. Execution order


1.2. Example

Interceptorcontroller.java

Package Com.ibigsea.springmvc.controller;import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.RequestMapping; @Controllerpublic class Interceptorcontroller {@ Requestmapping ("/interceptor") public String Interceptor () {SYSTEM.OUT.PRINTLN ("Controller Interceptor"); return " HelloWorld ";}}

Oneinterceptor.java

Package Com.ibigsea.springmvc.interceptor;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.web.servlet.handlerinterceptor;import Org.springframework.web.servlet.modelandview;public class Oneinterceptor implements Handlerinterceptor {/** * This method is called before the target method is executed.  * * If the return value is true, the subsequent interceptors and target methods continue to be invoked.  * If the return value is false, subsequent interceptors and target methods are no longer invoked.  * and execute the Aftercompletion () method that executes the Prehandle interceptor before the current interceptor * * Can do permissions, logs, transactions and other operations. */@Overridepublic Boolean prehandle (httpservletrequest request,httpservletresponse response, Object handler) throws Exception {System.out.println ("one Interceptor Prehandle"); return true;}  /** * After executing the target method, before rendering the view, * and Prehandle return to True, calling the method * can make modifications to the properties or views in the requesting domain. * Can add some public information such as the current time, such as */@Overridepublic void Posthandle (httpservletrequest request,httpservletresponse response, Object Handler,modelandview Modelandview) throws Exception {System.out.println ("one Interceptor Posthandle");} /** * The method is called after rendering the view and can be used to dispose of resources.*/@Overridepublic void aftercompletion (httpservletrequest request,httpservletresponse response, Object handler, Exception ex) throws Exception {System.out.println ("one Interceptor Aftercompletion");}}

Twointerceptor.java

Package Com.ibigsea.springmvc.interceptor;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.web.servlet.handlerinterceptor;import Org.springframework.web.servlet.modelandview;public class Twointerceptor implements Handlerinterceptor {@ Overridepublic boolean prehandle (httpservletrequest request,httpservletresponse response, Object handler) throws Exception {System.out.println ("Interceptor Prehandle"); return true;} @Overridepublic void Posthandle (httpservletrequest request,httpservletresponse response, Object Handler,modelandview Modelandview) throws Exception {System.out.println ("both Interceptor Prehandle");} @Overridepublic void Aftercompletion (httpservletrequest request,httpservletresponse response, Object handler, Exception ex) throws Exception {System.out.println ("both Interceptor Aftercompletion");}}

Spring-mvc.xml:

<mvc:interceptors><!--Configuring the Custom Interceptor--><bean id= "Oneinterceptor" class= " Com.ibigsea.springmvc.interceptor.OneInterceptor "/><bean id=" Twointerceptor "class=" Com.ibigsea.springmvc.interceptor.TwoInterceptor "/></mvc:interceptors>

Operation result :


One Interceptor Prehandletwo Interceptor Prehandlecontroller Interceptortwo Interceptor PreHandleone Interceptor Posthandletwo Interceptor Aftercompletionone Interceptor Aftercompletionone Interceptor PreHandletwo Interceptor Prehandle

1.3. Source Code Analysis






SPRINGMVC Study Notes (ix) custom interceptors

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.