SPRINGMVC Frame Interceptor

Source: Internet
Author: User

The Interceptor Interceptor in SPRINGMVC is also very important and useful, and its main function is to intercept the user's request and handle it accordingly.

First, define the Interceptor implementation class

The Interceptor interception request in SPRINGMVC is implemented through Handlerinterceptor . defining a interceptor in SPRINGMVC is very simple, mainly in two ways, the first way is to define the Interceptor class to implement the spring handlerinterceptor interface, Or this class inherits the classes that implement the Handlerinterceptor interface, such as what Spring has already provided to implement the Handlerinterceptor The handlerinterceptoradapter of an abstract class of interfaces , the second is to implement the Webrequestinterceptor interface of spring, or to inherit a class that implements Webrequestinterceptor.

(i) Implementation of the Handlerinterceptor interface

(1 ) prehandle  (httpservletrequest request, HttpServletResponse The response, Object handle) method, as the name implies, will be called before the request is processed. interceptor  in springmvc  is a chained invocation, where multiple interceptor  can exist in one application or in one request. Each interceptor  call is executed sequentially according to its declaration order, and the first execution is the prehandle  method in interceptor . Therefore, you can do some pre-initialization operations in this method or a preprocessing of the current request, or you can make some judgments in this method to determine whether the request should continue. The return value of the method is a Boolean of type bool, and when it returns to false , the request ends, and subsequent interceptor  and controller  are no longer executed; When the return value is true  will continue to invoke the next interceptor  prehandle  method, which is the controller  method that invokes the current request if it is already the last interceptor .

(2 ) posthandle  (httpservletrequest request, HttpServletResponse Response, Object handle, Modelandview Modelandview) method, explained by the prehandle  method we know this method includes the following aftercompletion  The interceptor  method can only be called if the return value of the prehandle  method of the currently-owned true  is the same. The posthandle  method, as the name implies, is executed after the current request is processed, that is, after the controller  method call, but it is called before the view is returned to render dispatcherservlet . So we can manipulate the modelandview  object after controller  processing in this method. The posthandle  method is called in the opposite direction to prehandle , which means that the posthandle  method of the first declaration is executed after the interceptor , and Struts2 The execution of the interceptor  inside the   is somewhat of a type. struts2  inside the interceptor  execution process is also chained, but in struts2  need to manually call actioninvocation  invoke  method to trigger a call to the next interceptor  or action , and then the contents of each interceptor  before the invoke  method call are executed in the order declared, and the Invoke The content behind the   method is reversed.

(3) Aftercompletion (HttpServletRequest request, httpservletresponse response, Object handle, Exception ex) method, This method is also required when the return value of the Prehandle method of the current corresponding interceptor is true to execute. As the name implies, the method executes after the entire request is finished, that is, after Dispatcherservlet renders the corresponding view. The main function of this method is to perform resource cleanup work.

(ii) Realization of the Webrequestinterceptor interface

Three methods are also defined in Webrequestinterceptor, and we are using these three methods to achieve interception. These three methods all pass the same parameter WebRequest, so what is this WebRequest? This WebRequest is an interface defined by spring, and its method definitions are basically the same as HttpServletRequest, in Webrequestinterceptor WebRequest All actions are synchronized to httpservletrequest and then passed in the current request.

(1) Prehandle (WebRequest request) method. The method will be called before the request is processed, meaning it will be called before the Controller method call. This method is different from the Prehandle in Handlerinterceptor, the main difference is that the return value of the method is void, that is, there is no return value, so we generally use it to prepare resources, for example, we are using hibernate You can prepare a Hibernate session object in this method, and then use WebRequest's setattribute (name, value, scope) to place it in the WebRequest attribute. Here's the third parameter to the SetAttribute method, scope, which is an integer type. In WebRequest's parent interface Requestattributes, it defines three constants:

Scope_request: Its value is 0, which means that it can be accessed only in REQUEST.

Scope_session: Its value is 1, if the environment allows it to represent a partial isolated session, otherwise it represents a normal session, and can be accessed within the session range.

Scope_global_session: Its value is 2, if the environment allows, it represents a global shared session, otherwise it represents the normal session, and within the scope of the session can be accessed.

(2) Posthandle (WebRequest request, Modelmap model) method. This method will be called after the request is processed, that is, after the controller method call, but will be called before the view returns are rendered, so you can change the presentation of the data by changing the data model Modelmap in this method. The method has two parameters, the WebRequest object is used to pass the entire request data, such as the data prepared in Prehandle can be passed through WebRequest and access; Modelmap is the model object returned by the controller after processing, We can change the returned model by changing its properties.

(3) Aftercompletion (WebRequest request, Exception Ex) method. The method is executed after the entire request process is completed, that is, after the view is returned and rendered. Therefore, the method can be used to release the resources of the operation. The WebRequest parameter can then pass the resources we have prepared in Prehandle to be released here. The Exception parameter represents the currently requested exception object, which is null if the exception thrown in the controller has already been handled by spring's exception handler.

Using the mvc:interceptors tag to declare a series of interceptors, then they can form an interceptor chain, the order of execution of the interceptor is executed in the order of the Declaration, the first declaration of the interceptor in the Prehandle method will be executed first, However, its posthandle methods and Aftercompletion methods are then executed.

There are two main ways of declaring interceptor under the Mvc:interceptors tab:

(1) A Bean object that directly defines a interceptor implementation class. Interceptor interceptors declared in this manner will intercept all requests.

(2) Use the Mvc:interceptor label for Declaration. Interceptor that are declared in this way can define the request path that needs to be intercepted through the mvc:mapping child tag.

After the above two steps, the defined interceptor will take effect to intercept the specific request.

* * from the following bloggers * *

SPRINGMVC Frame 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.