Definition of Spring Interceptor

Source: Internet
Author: User

(i), the definition of interceptors

1, why need interceptors: in the authentication or log records, we need to use the interceptor to achieve our goal

2. What's the Interceptor: in AOP (aspect-oriented programming), it is used to intercept a method or field before it is accessed, and then to add some actions before or after. Interception is an implementation strategy of AOP

3, how to use interceptors: in spring with interceptors need to implement the Handlerinterceptor interface or its implementation sub-class: Handlerinterceptoradapter, Also configure interceptors in the Applicationcontext.xml file

 PackageEdu.mybatis.interceptor;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.HandlerInterceptor;ImportOrg.springframework.web.servlet.ModelAndView; Public classMyhandlerintercepterImplementshandlerinterceptor{//before entering handler//identity authentication, identity authorization     Public BooleanPrehandle (httpservletrequest arg0, HttpServletResponse arg1, Object arg2)throwsException {System.out.println ("Lanjie=====1"); return true; }    //after entering handler, before returning to Modelandview//Modelandview: Uploading Common Model data (menu navigation) to the view     Public voidPosthandle (httpservletrequest arg0, HttpServletResponse arg1, Object arg2, Modelandview arg3)throwsException {System.out.println ("Lanjie=====1"); }    //after executing handler//Unified exception handling, unified log processing     Public voidaftercompletion (httpservletrequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throwsException {System.out.println ("Lanjie=====1"); } }

4. Configuring the Applicationcontext.xml File

<!--interceptors        --<mvc:interceptors> <!--multiple interceptors, sequential execution--        <mvc:interceptor>            <mvc:mapping path= "/**"/>            class= "Edu.mybatis.interceptor.HandlerIntercepter"/>        </mvc:interceptor>    </mvc:interceptors>

Here <mvc:mapping path= "..." > indicates the path of the file you want to intercept, you need to note when configuring the path:/** means all folders and their subfolders, and/* is all folders, not including subfolders

Definition of Spring 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.