Working with interceptors
As I already mentioned, interceptors are used to intercept actual web requests before or after they are processed. We can relate the interceptor's concept in Spring MVC with the filter concept of servlet programming. In Spring MVC, interceptors are the special classes that must implement the org.springframework.web.servlet.HandlerInterceptor
interface.
The HandlerInterceptor
interface defines three important methods, as follows:
preHandle
: This method gets called just before the web request reaches the controller method to be executedpostHandle
: This method will get called just after the execution of the controller methodafterCompletion
: This method will get called after the completion of the entire web request cycle
Once we have created our own interceptor by implementing the HandlerInterceptor
interface, we need to configure it in our web application context for it to take effect.