Spring MVC Interceptor Detailed

Source: Internet
Author: User

One: theoretical preparation

Interceptor : The Interceptor is a component that spring MVC provides, which can be executed after the client requests execution, and then logically processed after execution, similar to filter filters in the servlet.

Interceptor Action : Reference the execution time of the interceptor we can know its usefulness, like our common user login authentication, permission interception, etc. can be implemented with interceptors

Two: Custom interceptors

Spring MVC provides an interceptor interface, called Handlerinterceptor, when we need to use an interceptor, just implement the interface, rewrite the three methods inside, as shown in the following example:

public class Mysecondinterceptor implements Handlerinterceptor {

Private long start_time;
Private long end_time;

Before the processor executes the request
public boolean prehandle (HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
Start_time=system.currenttimemillis ();
System.out.println ("Before the second interceptor request ends: The second interceptor method has been implemented");
return true;
}
The end of the processor execution request, before the view layer is displayed (that is, before the controller sends the page to the front)
public void Posthandle (HttpServletRequest request,
HttpServletResponse response, Object handler,
Modelandview Modelandview) throws Exception {
System.out.println ("After the second interceptor request finishes: execution after the request ends" +modelandview);
System.out.println ("Back page:" +modelandview.getviewname ());
SYSTEM.OUT.PRINTLN ("Return Data:" +modelandview.getmodel (). Get ("key"));
You can also manipulate the results returned by the controller here
Modelandview.setviewname ("login");

}

After the processor has fully executed the request, the view has been shown
public void Aftercompletion (HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
Throws Exception {
End_time=system.currenttimemillis ();
System.out.println ("After the second interceptor request has completely ended: Login time Elapsed" + (End_time-start_time) + "MS");
}

}

Parameter description:

Request: Requests Processing interface

Response: Request Response Interface

Handler: Encapsulates the method that is called by the controller of the current request request and the return value type of the method

Ex: exception

Modelandview: View and data information returned by the controller

A description of the execution time for these three methods (if you do not understand spring MVC implementation principle or first understand the principle and then look at the Interceptor):

Prehandle: Before the request executes, a Boolean value is returned, true to continue execution (may be to continue execution of the next interceptor, if there is no interceptor to continue to execute the current request), false request interrupt, the method is usually used to perform permission interception, login authentication and other logic

Posthandle: After Dispartcherservlet (the host controller) calls the controller (Director), the call to Viewresolver (view resolver) is returned to the client page, it is important to note that the method is not necessarily executed, It must fulfill the following two conditions:

1) The Prehandle method of all interceptors returns true without throwing exceptions

2) All interceptor controller controllers do not throw an exception

One of the above conditions does not satisfy the method will not be executed, the method is usually used to perform common logic in the controller component, such as logging, etc.

Aftercompletion: Executes after the Dispartcherservlet returns the view to the front end, regardless of whether the controller throws an exception, this method typically implements performance monitoring of certain modules with prehandle, such as monitoring user login time

Three: Execution order and configuration

If spring MVC is equipped with multiple interceptors, their method execution is chained, and the order of Posthandle and Aftercompletion is the opposite of Prehandle. For example, there are now three interceptor a,b,c (order of the Interceptor's configuration order), each interceptor corresponding three methods are A1,A2,A3;B1,B2,B3,C1,C2,C3, then the execution order is

A1>b1>c1>c2>b2>a2>c3>b3>a3

If B1 returns false, the order is

A1>b1>c3

If the controller controllers of the B interceptor are thrown abnormally, the order of execution is

A1>b1>c1>c3>b3>a3

You can write your own interceptor to verify, not more than repeat

IV: Data returned by the Operation Controller

Friends familiar with spring MVC should know that there are two types of controller return results: string and Modelandview,

If the controller returns both types of data, we can easily use the parameters in the Posthandle method to manipulate the data before the host controller renders the view and data information to the client Modelandview. But usually we now have a Web project that uses @responsebody annotations to return our custom types of data to the front end, So at this point we'll find the parameters in the Posthandel method Modelandview can't receive the view and data information returned by the controller, so what do we do now?

Suppose the controller we customize the returned type to Jsonresult

Define a class implementation responsebodyadvice, as shown below

@ControllerAdvice
public class Myresponsebodyadvice implements responsebodyadvice<jsonresult>{

Public Boolean supports (Methodparameter returntype,
class<? Extends Httpmessageconverter<?>> Convertertype) {
Let the request continue execution, return false by default, and let it return true to continue execution
return true;
}

Public Jsonresult Beforebodywrite (Jsonresult body,
Methodparameter ReturnType, mediatype Selectedcontenttype,
class<? Extends Httpmessageconverter<?>> Selectedconvertertype,
ServerHTTPRequest request, serverhttpresponse response) {

SYSTEM.OUT.PRINTLN ("Returned JSON data" +body.tostring ());
Body.setmessage ("Modify the returned message data");
return body;
}

}

It is important to note that this class must add @controlleradvice annotations to ensure that each @controller executes the method inside the class, so his execution sequence is after the controller, before the Posthandle.



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.