SPRINGMVC Learning (12) interceptors in--SPRINGMVC
SPRINGMVC processor interceptors are similar to filter filters in servlet development for preprocessing and post-processing of processors. This paper summarizes how interceptors are defined in SPRINGMVC, as well as the execution and use of the interceptor.
Definition and configuration of interceptors in SPRINGMVC definition of interceptors in SPRINGMVC
In Springmvc, define the interceptor to implement the Handlerinterceptor interface, and implement the three methods provided in the interface, as follows:
Public classInterceptor1ImplementsHandlerinterceptor {@Override Public BooleanPrehandle (httpservletrequest request, httpservletresponse response, Object handler)throwsException {System.out.println ("Interceptor1 prehandle ..."); //The time for execution is to execute this method before handler executes//return Value: If True, release, do not intercept, normal execution handler for processing//return value: If you return false, then intercept, handler will not be able to handle the normal return true; } @Override Public voidPosthandle (httpservletrequest request, httpservletresponse response, Object handler, Modelandview Modeland View)throwsException {//after the handler is executed, do something to Modelandview before returning to ModelandviewSystem.out.println ("Interceptor1 posthandle ..."); } @Override Public voidaftercompletion (httpservletrequest request, httpservletresponse response, Object handler, Exception ex) /c10>throwsException {//after returning to Modelandview//handler an exception occurred during execution, you can handle the exception hereSystem.out.println ("Interceptor1 aftercompletion ..."); }}
For these three methods, I do a simple analysis:
- Prehandle method: The time that the method executes is executed before the handler executes. can be used for identity authentication, identity authorization, and so on. For example, if authentication does not pass indicating that the user is not logged in, this method is required to intercept the execution (return false), otherwise it will be released (return true).
- Posthandle method: The timing of this method is executed after handler is executed, and you can see that there is a Modelandview parameter in the method before returning to Modelandview. Application scenario: From Modelandview, the common model data (such as menu navigation) is uploaded to the view here, or the view can be specified uniformly here.
- Aftercompletion method: Executes after returning Modelandview. Application scenario: Uniform exception handling (that is, exceptions occur during handler execution, exceptions can be handled here), unified log processing, and so on.
SPRINGMVC The Interceptor configuration for some kind of handlermapping configuration interceptor
In Springmvc, interceptors are configured for specific handlermapping, that is, if an interception is configured in a handlermapping, handler that passes the handlermapping mapping will eventually use the interceptor. For example, suppose we configure the mapper in the Springmvc.xml configuration file org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
, then we can configure the interceptor like this:
class= "Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" > <property name= " Interceptors "> <list> <ref bean=" HandlerInterceptor1 "/> <ref bean=" HandlerInterceptor2 "/> </list> classclass=" Com.itheima.springmvc.interceptor.HandlerInterceptor2 "/>
Configuring global interceptors for all handlermapping
So how do you configure a global-like interceptor in SPRINGMVC? It also says that the interceptor in SPRINGMVC is specific to the mapper, in order to solve this problem, the SPRINGMVC framework will be configured to inject a similar global interceptor into each handlermapping, so that it can become a global interceptor. The configuration is as follows:
<!--configuring interceptors--><mvc:interceptors> <!--executing interceptors in the order configured-- <mvc:interceptor> < Mvc:mapping path= "/**"/> class= "Com.itheima.springmvc.interceptor.Interceptor1" ></bean > </mvc:interceptor> <mvc:interceptor> <mvc:mapping path= "/**"/> class = "Com.itheima.springmvc.interceptor.Interceptor2" ></bean> </MVC:INTERCEPTOR></MVC: Interceptors>
Note: path= "/**" means to intercept all URLs including child URL paths. In the actual development, we usually use this configuration, <mvc:mapping>
specify the URL to intercept.
SPRINGMVC Interceptor Execution Test
Follow the above HandlerInterceptor1 and then write two interceptors, HandlerInterceptor2 and HANDLERINTERCEPTOR3, CONFIGURED according to the above configuration, and then we will test the implementation of three interceptors, and do a related summary.
Three interceptors are released.
That is, we change the return value in the Prehandle method of the three interceptor to true, to test the order of execution of the interceptor, to access any URL, the test result will be as follows:
Interceptor1 Prehandle ... Interceptor2 Prehandle ... Interceptor3 Prehandle ... Interceptor3 Posthandle ... Interceptor2 Posthandle ... Interceptor1 Posthandle ... Interceptor3 aftercompletion ... Interceptor2 aftercompletion ... Interceptor1 aftercompletion ...
Make a summary based on the printed results: When all interceptors are released, the Prehandle method is executed in the order configured, while the other two methods are executed in reverse order.
There's an interceptor that doesn't release.
We will change the return value in the Prehandle method of the third interceptor to false, the first two or true, to test the order of execution of the interceptor, to access any URL, the test results will be as follows:
Interceptor1 Prehandle ... Interceptor2 Prehandle ... Interceptor3 Prehandle ... Interceptor2 aftercompletion ... Interceptor1 aftercompletion ...
Make a summary based on the printed results:
Because interceptors 1 and 2 are released, the Prehandle of the Interceptor 3 can be executed. This means that the front interceptor is released and the interceptor behind it can execute the Prehandle method.
Interceptor 3 is not released, so the other two methods are not executed. That is, if an interceptor is not released, the other two methods will not be executed.
As long as an interceptor is not released, the Posthandle method of all interceptors will not be executed, but as long as the Prehandle is executed and released, the Aftercompletion method will be executed.
None of the three interceptors are released.
This situation can actually refer to the above situation, is a special case, also look at the results of the operation:
Interceptor1 Prehandle ...
Obviously, only the Prehandle method of the first interceptor is executed, because none is released, so there is no one to execute the Posthandle method and Aftercompletion method.
Summarize
- Prehandle called by interceptor definition order
- Posthandler by interceptor definition reverse Call
- Aftercompletion by interceptor definition reverse Call
- Posthandler all interceptors within the interceptor chain return true to call
- Aftercompletion only Prehandle returns true to call
Application of Interceptors
In the second case, such as now to write a unified exception handling logic, then to put the interceptor in the first position of the chain, and must be released, because only release, will be executed aftercompletion, and placed in the first interceptor chain, The Aftercompletion method is executed last to execute the logic of uniform exception handling inside.
For example, login to the authentication interceptor, to be placed in the first position of the Interceptor link (if there is a unified exception handling, it should be placed behind the unified exception processing); The permission check blocker is placed after the login authentication interceptor (because the permission is verified after login). But here I'm just going to write a login-verified interceptor to show how to use the SPRINGMVC interceptor.
Processing flow
- First you have to have a login page, and then you need to write a controller class to access the page
- The login page has an action to submit the form, which is also required to be handled in the Controller class
- A) determine if the user name password is correct
- b) If correct, write user information to session
- c) Return to the Login Success page, or jump to the Product list page
- Handling of interceptors
- A) Intercept user requests and determine if the user is logged in
- b) If the user is already logged in, release
- c) If the user is not logged in, jump to the login page
Provide a login page
Write a login.jsp login page under the/web-inf/jsp directory, as follows:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Write a controller that implements user loginWrite the Usercontroller that implements the user login under the Com.itheima.springmvc.controller package, as follows:
Public classUsercontroller {@RequestMapping ("/user/showlogin") PublicString Showlogin () {return"Login"; } @RequestMapping ("/user/login") Publicstring Userlogin (string username, string password, HttpSession session) {//a) Determine if the user name password is correctSystem.out.println (username); SYSTEM.OUT.PRINTLN (password); //b) If correct, write user information to sessionSession.setattribute ("username", username); //c) Return to the Login Success page, or jump to the Product List page return"Redirect:/item/itemlist.action"; }}
Implementation of login Verification interceptorUnder the Com.itheima.springmvc.interceptor package, write a login-validated interceptor--logininterceptor.java, as follows:
Public classLogininterceptorImplementsHandlerinterceptor {@Override Public BooleanPrehandle (httpservletrequest request, httpservletresponse response, Object handler)throwsException {String uri=Request.getrequesturl (). toString (); //If the user is not logged in, then jump to the login page, the process of jumping may be intercepted, so you have to make a judgment if(Uri.contains ("Login")) { return true; } System.out.println ("Interceptor1 prehandle ..."); //a) Intercept user requests and determine if the user is logged inHttpSession session =request.getsession (); Object username= Session.getattribute ("username"); if(Username! =NULL) { //b) If the user is already logged in. Release return true; } //c) If the user is not logged in, jump to the login page, the process of jumping may be intercepted, so you have to make a judgmentResponse.sendredirect (Request.getcontextpath () + "/user/showlogin"); return false; } @Override Public voidPosthandle (httpservletrequest request, httpservletresponse response, Object handler, Modelandview Modelan DView)throwsException {//after the handler is executed, do something to Modelandview before returning to ModelandviewSystem.out.println ("Interceptor1 posthandle ..."); } @Override Public voidaftercompletion (httpservletrequest request, httpservletresponse response, Object handler, Exception ex)
throwsException {//after returning to Modelandview//handler an exception occurred during execution, you can handle the exception hereSystem.out.println ("Interceptor1 aftercompletion ..."); }}
Then configure the Interceptor:
<!--configuring interceptors--><mvc:interceptors> <mvc:interceptor> <mvc:mapping path= "/**"/> class= "Com.itheima.springmvc.interceptor.LoginInterceptor" ></bean> </mvc:interceptor ></mvc:interceptors>
When we arbitrarily request a URL, we will be captured by the interceptor we have just defined, and then we will determine whether there is user information in the session, and then skip to the landing page and let us log in.
SPRINGMVC Learning (12) interceptors in--SPRINGMVC