Interceptors are popular in the Struts 2 framework, but Spring MVC can implement interceptors to manage sessions, logging, and auditing a user authentication process. The concept of an interceptor is similar to the filters in a typical Java Enterprise model but not exactly the same.
In order to create a handler interceptor, we must make use of org.springframework.web.servlet.HandlerInterceptor
. The HandlerInterceptor
defines three methods that can be used for pre-processing and post-processing requests:
PreHandle
: This is called before a URL is executed, and returns a Boolean value. When it returns true, the handler execution chain continues, whereas when it returns false, theDispatcherServlet
stops the execution of the entire request-response transaction.PostHandle
: This is called after a URL is executed.AfterCompletion
: This is called after the result view page has been executed.
The SMP project uses interceptors to check the number of visitors to the portal, the duration...