Use of Spring mvc_handlerinterceptoradapter (not only for all functions of filter, but also for more precise control of interception accuracy)

Source: Internet
Author: User
use of Spring mvc_handlerinterceptoradapter

In general, the interception of requests from the browser is implemented using filter, which enables Bean preprocessing and post-processing.
Spring MVC interceptors not only implement all the functions of filter, but also control interception accuracy more precisely.
Spring provides us with the Org.springframework.web.servlet.handler.HandlerInterceptorAdapter adapter that inherits this class and makes it very easy to implement your own interceptors. He has three methods:

Java code   Public boolean prehandle (httpservletrequest request, httpservletresponse  Response, object handler)               throws exception {             return  true;         }          Public void posthandle (                  httpservletrequest request, httpservletresponse response, object  handler, modelandview modelandview)                   throws Exception {          }         public void aftercompletion (                  httpservletrequest request,  HTTPSERVLETRESPONSE RESPONSE, OBJECT HANDLER, EXCEPTION EX)                  throws Exception {          }    

preprocessing, post processing (call service and return Modelandview, but no page rendering), return processing (page already rendered)

In the Prehandle, the coding, security control and so on can be processed;
In Posthandle, there is an opportunity to modify the Modelandview;
In aftercompletion, logging is possible based on whether an ex is null to determine if an exception has occurred.

If you use spring MVC based on an XML configuration,
You can use simpleurlhandlermapping, beannameurlhandlermapping for URL mapping (equivalent to Struts's path mapping) and interception requests (injected interceptors).
If you use SPRINGMVC based on annotations, you can use defaultannotationhandlermapping to inject interceptors.
Note whether XML-based or annotation-based, Handlermappingbean is required to be configured in XML.

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.