(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