SPRINGMVC's Interceptor

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.