1: Write a custom Interceptor. Inherit Handlerinterceptor
1 PackageCom.ssm.interceptor;2 3 ImportOrg.springframework.web.servlet.HandlerInterceptor;4 ImportOrg.springframework.web.servlet.ModelAndView;5 6 Importjavax.servlet.http.HttpServletRequest;7 ImportJavax.servlet.http.HttpServletResponse;8 9 Public classMyinterceptorImplementsHandlerinterceptor {Ten One /* A execution Time: The Prehandle method of the interceptor is first reached. - This method is executed before the target method. - return True to execute normally. the Return False if the return is false, subsequent executions will not be performed. - role: Permissions are done in this method. Operations such as logs, transactions, and so on. - */ - Public BooleanPrehandle (HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object o)throwsException { +System.out.println ("Prehandle Method of Interceptors"); - return true; + } A /* at Execution Time: The method that the target method executes before rendering the view. - function: Used to request the modification of a property or view in a domain. - */ - Public voidPosthandle (HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object o, ModelAndView Modelandview)throwsException { -System.out.println ("Posthandle Method of Interceptors"); - } in /* - execution time: After rendering the view. to role: Frees resources. + */ - the Public voidAftercompletion (HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object o, Exception E )throwsException { *System.out.println ("Aftercompletion Method of Interceptors"); $ }Panax Notoginseng}
It is then configured on the SPRING-MVC XML file. That is, in the spring IOC.
1 <mvc:interceptors>2 <mvc:interceptor>3 < which interceptor is added to!-- >4 <mvc:mapping path= "/emps"/>5 class= " Com.ssm.interceptor.SecondInterceptor "/>6 </mvc:interceptor>7 </ Mvc:interceptors>
Common scenarios
1, log records: Log the request information for information monitoring, information statistics, calculation of PV (Page View) and so on.
2, permission check: such as login detection, enter the processor detection detection is logged in, if not directly back to the login page;
3, performance monitoring: Sometimes the system in a certain period of time inexplicably slow, can pass through the interceptor before entering the processor before the start time, record the end time after processing, so as to get the processing time of the request (if there is a reverse proxy, such as Apache can automatically record);
4, general behavior: Read the cookie to get user information and put the user object into the request, so as to facilitate the subsequent use of the process, as well as the extraction locale, theme information, as long as the multiple processors are required to use the interceptor implementation.
Order of execution of multiple interceptors:
The first interceptor return false:
None of the back will be executed.
A second interceptor return false:
SPRINGMVC's Interceptor