Overview
Part 1th Interceptor Class
Part 2nd Interceptor Configuration
Part 3rd Example
Reference
Part 1th Interceptor Class
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:
1 Public BooleanPrehandle (httpservletrequest request, httpservletresponse response, Object handler)2 throwsException {3 return true; 4 } 5 Public voidPosthandle (6 httpservletrequest Request, httpservletresponse response, Object handler, Modelandview Modelandview) 7 throwsException {8 } 9 Public voidAftercompletion (Ten httpservletrequest Request, httpservletresponse response, Object handler, Exception ex) One throwsException { A}
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.
Part 2nd Interceptor Configuration
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 spring MVC based on annotations, you can use defaultannotationhandlermapping to inject interceptors.
Note whether XML-based or annotation-based, the handlermapping bean needs to be configured in XML.
The annotation-based configuration is as follows:
< mvc:interceptors > < class= "com. Common.action.PurviewInterceptTest "/> </mvc:interceptors >
Part 3rd Example
/*** Interceptor Test * * @ClassName: Purviewintercepttest *@authorxingle * @date 2015-3-6 pm 12:02:08*/ Public classPurviewintercepttestextendsHandlerinterceptoradapter {PrivateUrlpathhelper Urlpathhelper =NewUrlpathhelper (); Public BooleanPrehandle (httpservletrequest request, httpservletresponse response, Object handler)throwsException {//Get access URLString URL =urlpathhelper.getlookuppathforrequest (Request); String queryString=urlpathhelper.getoriginatingquerystring (Request); String RedirectURL=URL; if(!Stringhelpertools.isempty (queryString)) {RedirectURL= RedirectURL + "?" +queryString; } if(true) {//test Jump to another pageResponse.sendredirect (Request.getcontextpath () + "/login1.html"); return true; } return Super. Prehandle (Request, response, handler); } Public voidPosthandle (httpservletrequest request, httpservletresponse response, Object handler, org.spring Framework.web.servlet.ModelAndView Modelandview)throwsException {//your own business logic//don't move the following sentence, just put it like this. After you have processed your business logic, spring will continue to pass your request and response to the container or to the client. Super. Posthandle (Request, response, Handler, Modelandview); } Public voidaftercompletion (httpservletrequest request, httpservletresponse response, Object handler, Exception ex) throwsException {//your own business logic//don't move the following sentence, just put it like this. After you have processed your business logic, spring will continue to pass your request and response to the container or to the client. Super. Aftercompletion (Request, response, Handler, ex); }}
Reference:
An explanation of the 1.spring MVC interceptor and <mvc:annotation-driven/>
The use of Handlerinterceptoradapter in Spring MVC