SPRINGMVC's Interceptor

Source: Internet
Author: User

The controller layer interceptor inherits from the Handlerinterceptoradapter

The Handlerinterceptoradapter.java code is as follows

 Public Abstract classHandlerinterceptoradapterImplementsHandlerinterceptor { Public BooleanPrehandle (HttpServletRequest request, httpservletresponse response, Object handler)throwsException {return true; }      Public voidPosthandle (httpservletrequest request, httpservletresponse response, Object handler, Modelandview Modelandview) throwsException {
} Public voidaftercompletion (httpservletrequest request, httpservletresponse response, Object handler, Exception ex)throwsException {
} }

The spring interceptor implements a controller interception by overriding these three methods.

  Public classControllerinterceptorextendsHandlerinterceptoradapter {        Public BooleanPrehandle (httpservletrequest request,HttpServletResponse response, Object handler)throwsException {System.out.println ("Controllerinterceptor.prehandle ()");       return true;}        Public voidPosthandle (httpservletrequest request,httpservletresponse Response, Object handler,Modelandview Modelandview)throwsException {System.out.println ("Controllerinterceptor.posthandle ()");}        Public voidaftercompletion (httpservletrequest request,httpservletresponse Response, Object handler, Exception ex)throwsException {System.out.println ("Controllerinterceptor.aftercompletion ()");}}

Configuring interceptors

<BeanID= "Handlermapping"class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">< Propertyname= "Interceptors"><List><refBean= "Controllerinterceptor" /></List></ Property>< Propertyname= "Mappings"><Props><propKey= "/hao/hello.do">Helloworldcontroller</prop></Props></ Property></Bean> <BeanID= "Controllerinterceptor"class= "Com.web.spring.mvc.interceptor.ControllerInterceptor"/>


Test Code Helloworldcontroller.java

@Controller @requestmapping ("/hao")  Public class  = "/hello")   public String Hello (httpservletrequest request, HttpServletResponse Response) {System.out.println ("Hello"); return  "HelloWorld"; }}

Description
Initiate the request, go into the interceptor chain, run the Prehandle method of all interceptors,
1. When the Prehandle method returns False, the Aftercompletion method of all interceptors is executed back from the current interceptor before exiting the interceptor chain.
2. When the Prehandle method is all true, execute the next interceptor until all interceptors are executed. Run the intercepted controller again. Then go into the interceptor chain, run the Posthandle method of all interceptors, and execute the Aftercompletion method of all interceptors back from the last interceptor.
When an interceptor throws an exception, the Aftercompletion method of all interceptors is executed back from the current interceptor

SPRINGMVC's 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.