The use of the SPRINGMVC mvc:interceptors interceptor

Source: Internet
Author: User

Reference: Thank you for your hard finishing!

68487904

Https://www.cnblogs.com/lcngu/p/7096597.html

Https://www.cnblogs.com/daimajun/p/7172208.html

72770853

Want to do a detailed collation of the use of the Spring MVC Interceptor, search from the Internet, found that someone is well-organized (https://www.cnblogs.com/lcngu/p/7096597.html), special record as follows

1. How interceptors are implemented

In the Spring framework, we want to implement the function of the interceptor, mainly through two ways, the first is to implement the HandlerInterceptor interface, the second is to implement the WebRequestInterceptor interface.

The first method is more commonly used, in the HandlerInterceptor interface, the definition of 3 methods, respectively preHandle() , postHandle() and afterCompletion() , we are through the replication of these 3 methods to intercept the user's request processing. Therefore, we can HandlerInterceptor realize the function of the interceptor by implementing the interface directly.

2. Configuring interceptors

In the Spring MVC project configuration file Dispatcher-servlet.xml, add the interceptor configuration as follows

<!--configuring interceptors for session validation -<!--If there are multiple interceptors that meet the requirements of interception processing, they are executed according to the order of the configuration -<mvc:interceptors>    <Mvc:interceptor>        <!--interception of all requests, this must be written in front, that is, written on the "Do not intercept" above -        <mvc:mappingPath="/**" />        <!--but excluding the following, that is, not intercepting the request -        <mvc:exclude-mappingPath= "/login.html" />        <mvc:exclude-mappingPath= "/account/login.do" />        <mvc:exclude-mappingPath= "/account/regist.do" />        <!--Interceptor Java code path -        <Beanclass= "Com.msym.cloudnote.interceptors.LogsInterceptor" />    </Mvc:interceptor></mvc:interceptors>

"description":

1) mvc:mapping Interceptor path configuration, where/** means all folders and subfolders inside, (/* is all folders, no subfolders,/is the root of the Web project, the role of the three needs to be studied in depth)

2) Mvc:exclude-mapping Interceptor does not need to intercept the path

3) mvc:mapping (intercept) must be written before mvc:exclude-mapping

3. Interceptor Code

 Public classLogsinterceptorextendsHandlerinterceptoradapter {Private Static FinalLogger Logger = Loggerfactory.getlogger (logsinterceptor.class); PrivateNamedthreadlocal<string> Logcontext =NewNamedthreadlocal<string> ("Log-id"); @AutowiredPrivateTlogdao Logdao; /*** The Prehandle method is used for processor interception, as the name implies, the method will be called before the controller processing, * SPRINGMVC Interceptor Interceptor is chained, you can have multiple interceptor at the same time,     * Then SPRINGMVC will execute one after the other according to the order of the Declaration, * and all prehandle methods in the interceptor will be called before the Controller method call.     * SPRINGMVC This interceptor chain structure can also be interrupted, * This mode of interruption is to make the return value of Prehandle is false, when the return value of Prehandle is false, the entire request is over. */@Override Public BooleanPrehandle (HttpServletRequest request, httpservletresponse response, Object handler)throwsException {String host=Request.getremotehost (); String URL=Request.getrequesturi (); Tlogentity Entity=Newtlogentity (); Entity.setcreatetime (NewTimestamp (System.currenttimemillis ())); Entity.setcreateuser ("Admin");        Entity.setipaddress (host);        Entity.setlogurl (URL); Entity.setissuccess (N);        Logdao.save (entity);        Logcontext.set (Entity.getlogid ()); Logger.debug ("IP for---->>>" + "host +" <<<-----access to the system "); return true; }    /*** This method will only be executed if the current interceptor Prehandle method returns a value of true. * Posthandle is used for processor interception, its execution time is after the processor processing, that is, after the controller's method call execution, * but it will be executed before the rendering of the view Dispatcherservlet,     This means that you can operate on the Modelandview in this method. * The chain structure of this method is opposite to the normal access direction, that is, the first declaration of the Interceptor Interceptor this method will be called later, * This is a bit like Struts2 inside the execution of the interceptor, * just Struts2 inside the Intercept method to manually Call the Invoke method of Actioninvocation, * The Invoke method of calling Actioninvocation in Struts2 is to call the next interceptor or call the action, * and then in Interceptor     The contents of the previous call are written before invoking invoke, and the content to be called after Interceptor is written after invoking the Invoke method. */@Override Public voidPosthandle (HttpServletRequest request, httpservletresponse response, Object handler, Modelandview Modelandview)throwsException {}/*** This method is also required when the return value of the Prehandle method of the current corresponding interceptor is true before execution. * This method will render the view execution after the entire request is completed, that is, the main function of this method is to clean up the resources, Dispatcherservlet*/@Override Public voidaftercompletion (httpservletrequest request, httpservletresponse response, Object handler, Exception ex) {Stri NG host=Request.getremotehost (); String Logid=Logcontext.get (); Tlogentity Entity=Logdao.findone (Logid); Entity.setissuccess (Y);        Logdao.save (entity); Logger.debug ("IP is---->>>" + Host + <<<-----Access succeeded "); }}

4. The order in which Prehandle () and Posthandle () are run in the Interceptor

The use of the SPRINGMVC mvc:interceptors 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.