There are two ways to configure spring interceptors
1. Implement Interface: Handleinterceptor
public class MyInterceptor1 implements Handlerinterceptor {@Overridepublic void aftercompletion (httpservletrequest Request, httpservletresponse response, Object obj, Exception e) throws Exception {System.out.println ("last run.!! Typically used to release resources. ");} @Overridepublic void Posthandle (HttpServletRequest request, httpservletresponse response, Object obj, Modelandview Model) throws Exception {System.out.println ("After the action is run, run before build view!! ");} @Overridepublic boolean prehandle (httpservletrequest request, httpservletresponse response, Object obj) throws Exception {System.out.println ("Run before" Action!!
! "); return true; Continue to run the action}}
A) Prehandle
This method runs before the action is run. It is possible to preprocess the data. For example: coding, security control, and so on.
Assuming the method returns true, the action continues to run.
b) Posthandle
This method runs after the action is run, before the view is generated. Over here. We have the opportunity to change the view layer data.
c) aftercompletion
Last run. Often used to dispose of resources and handle exceptions.
We are able to do the related exception handling based on whether the ex is empty.
Since we normally handle exceptions, we throw exceptions from the bottom up. Finally, the spring framework has come to this approach.
2. Inheritance adapter: Handleinterceptoradapter
public class MyInterceptor2 extends Handlerinterceptoradapter {@Overridepublic Boolean prehandle (httpservletrequest Request, HttpServletResponse response, Object handler) throws Exception {System.out.println ("Myinterceptor2.prehandle () "); return true; Continue to run the action}}
3. Configure User-servlet.xml
Scenario 1:
<!--Configure yourself to define the Interceptor--><mvc:interceptors><bean class= "Com.zdp.interceptor.MyInterceptor1" ></bean > <!--intercept all url!--></mvc:interceptors>
Scenario 2:
<!--Configure yourself to define the Interceptor--><mvc:interceptors><mvc:interceptor><mvc:mapping path= "/user/add"/> <! --just intercept join user--><bean class= "Com.zdp.interceptor.MyInterceptor2" ></bean></mvc:interceptor> </mvc:interceptors>
SPRINGMVC's Interceptor