Spring provides us with:
Org.springframework.web.servlet. Handlerinterceptor Interface,
Org.springframework.web.servlet.handler. Handlerinterceptoradapter Adapter,
Implement this interface or inherit this class, it is very convenient to implement their own interceptors.
For example:
Public classHelloworldinterceptorImplementsHandlerinterceptor {@Override Public BooleanPrehandle (httpservletrequest request, httpservletresponse response, Object handler)throwsException {System.out.println ("Pre-handle"); return false; } @Override Public voidPosthandle (httpservletrequest request, httpservletresponse response, Object handler, Modelandview m Odelandview)throwsException {System.out.println ("Post-handle"); } @Override Public voidaftercompletion (httpservletrequest request, httpservletresponse response, Object handler, Exception ex) throwsException {System.out.println ("After completion handle"); }
There are three ways to do this:
Action before executing:
public boolean prehandle (HttpServletRequest request, httpservletresponse response, Object handler);
Execute before generating view
public void Posthandle (HttpServletRequest request, httpservletresponse response, Object handler, Modelandview Modelandview);
Last execution, can be used to release resources
public void Aftercompletion (httpservletrequest request,httpservletresponse response, Object handler, Exception ex)
preprocessing, post processing (call service and return Modelandview, but no page rendering), return processing (page already rendered)
In the Prehandle, the coding, security control and so on can be processed;
In Posthandle, there is an opportunity to modify the Modelandview;
In aftercompletion, logging is possible based on whether an ex is null to determine if an exception has occurred.
The object handler in the parameter is the next interceptor.
There are three ways to configure the spring MVC configuration file:
Scenario one, (approximate) total Interceptor, block all URLs
In the case of restful URLs, static resources are also intercepted.
< mvc:interceptors > < class= "Com.app.mvc.MyInteceptor"/> </MVC: Interceptors>
Scenario two, intercept the matching URLs.
In the case of restful URLs, static resources are also intercepted.
<mvc:interceptors> <Mvc:interceptor> <mvc:mappingPath= "/user/*" /> <!--/user/* - <Beanclass= "Com.mvc.MyInteceptor"></Bean> </Mvc:interceptor> </mvc:interceptors>
Scenario three, the interceptor on the Handlermappint.
<Beanclass= "Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> < Propertyname= "Interceptors"> <List> <Beanclass= "Com.mvc.MyInteceptor"></Bean> </List> </ Property> </Bean>
If <mvc:annotation-driven/> is used, it automatically registers defaultannotationhandlermapping with Annotationmethodhandleradapter These two beans, so there is no chance to inject it with the interceptors attribute, you cannot specify an interceptor.
We can inject the interceptors attribute into the interceptor by manually configuring the above two beans without using <mvc:annotation-driven/>.