Interceptor is an object that is invoked at the preprocessing and post processing of a request. In Struts 2, interceptor is used to perform operations such as validation, exception handling, internationalization, displaying intermediate result etc.
ActionInvocation is responsible to encapsulate Action classes and interceptors and to fire them in order. The most important method for use in ActionInvocation is invoke() method that keeps track of the interceptor chain and invokes the next interceptor or action. This is the best example of Chain of Responsibility pattern in J2EE.
Interceptors are conceptually the same as servlet filters or the JDKs Proxy class.
Advantage of interceptors
Interceptor plays a crucial role in achieving high level of separation of concerns.
Providing preprocessing/post processing logic before/after the action is called.
Catching exceptions so that alternate processing can be performed.
Pluggable if we need to remove any concern such as validation, exception handling, logging etc. from the application, we don't need to redeploy the application. We only need to remove the entry from the struts.xml file.
Which design pattern is implemented by Struts2 interceptors?
Struts2 interceptors are based on intercepting filters design pattern.
The invocation of interceptors in interceptor stack closely resembles Chain of Responsibility design pattern.