Spring MVC Interceptor

Source: Internet
Author: User

First, the concept of interceptors:

The Interceptor in Java is the object that dynamically intercepts the action call. It provides a mechanism to enable developers to define code that executes before and after an action executes, or to prevent it from executing before an action is executed.

It also provides a way to extract the reusable parts of an action. In AOP (aspect-oriented programming) interceptors are used before a method or field is accessed,

To intercept and then add some actions before or after.

Second, the working principle:

PRINGMVC has a unified entrance Dispatcherservlet, all requests through dispatcherservlet, so only need to be on the dispatcherservlet of the fuss, Dispatcherservlet also has no agent,

At the same time SPRINGMVC management controller also does not have the agent. It's not hard to imagine that we did some action before we executed the controller, performed some action, and render finished doing some action.

The SPRINGMVC interceptor corresponds to three prehandle,posthandle,aftercompletion methods. Just write the logic we need in three ways, it's all crap.

Third, the use of the interceptor scene:

Principle of use: handling common issues with all requests

1, solve garbled problem

2. Resolve Permissions Validation Issues

Iv. the difference between interceptors and filters:

Filters can be simply understood as "take what you want", ignoring things you don't want, and interceptors can be simply understood as "refusing what you want" and caring about what you want to reject.

1. Interceptors are based on the Java reflection mechanism, and filters are based on function callbacks.

2. The filter relies on the servlet container, and the interceptor does not depend on the servlet container.      3. The interceptor only works on the action, and the filter can work on almost all requests.      4. The interceptor can access the action context, the object in the value stack, and the filter cannot. 5. In the life cycle of an action, interceptors can be called multiple times, and filters can only be called once when the container is initialized.
Five, the implementation of interceptors:
1. Write the Interceptor class to implement the Handinterceptor interface:

2. Register the Interceptor in the Spring MVC framework:

3. Configure interceptor Interception rules:
(1)

(2)

Six, the Interceptor method introduction:
1, Prehandle: Preprocessing callback method, implementation of processor preprocessing (such as login check), the third parameter is the response of the processor (as in our previous chapter controller implementation);
Return value: True indicates continuation of the process (such as invoking the next interceptor or processor);
False indicates a process interruption (such as a login check failure) and does not continue to invoke other interceptors or processors, at which point we need to generate a response via response;
2. Posthandle: Post-processing callback method to implement post processing of the processor (but before rendering the view), at which point we can pass Modelandview (model and view object)
Modelandview can also be null if the model data is processed or the view is processed.
3, Aftercompletion: The entire request processing completed callback method, that is, when the view rendering is complete callback, such as performance monitoring we can record the end time and output consumption time, there are some resources to clean up,
Similar to finally in Try-catch-finally, but only invokes the aftercompletion of the interceptor in the processor execution chain that Prehandle returns TRUE.


Spring MVC 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.