use of Spring mvc_handlerinterceptoradapter
In general, the interception of requests from the browser is implemented using filter, which enables Bean preprocessing and post-processing.
Spring MVC interceptors not only implement all the functions of filter, but also control interception accuracy more precisely.
Spring provides us with the Org.springframework.web.servlet.handler.HandlerInterceptorAdapter adapter that inherits this class and makes it very easy to implement your own interceptors. He has three methods:
Java code Public boolean prehandle (httpservletrequest request, httpservletresponse Response, object handler) throws exception { return true; } Public void posthandle ( httpservletrequest request, httpservletresponse response, object handler, modelandview modelandview) throws Exception { } public void aftercompletion ( httpservletrequest request, HTTPSERVLETRESPONSE RESPONSE, OBJECT HANDLER, EXCEPTION EX) throws Exception { }
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.
If you use spring MVC based on an XML configuration,
You can use simpleurlhandlermapping, beannameurlhandlermapping for URL mapping (equivalent to Struts's path mapping) and interception requests (injected interceptors).
If you use SPRINGMVC based on annotations, you can use defaultannotationhandlermapping to inject interceptors.
Note whether XML-based or annotation-based, Handlermappingbean is required to be configured in XML.