Struts 2 Interceptors Examples

Struts 2 Custom Interceptor Example

In apache struts 2 framework there are two categories of the interceptors default and custom. The default interceptors need not to be create its provided by the struts 2 , we can create the custom interceptor by implementing the Interceptor interface in a class and overriding its three life cycle method as the following given in the given example .

For creating the custom interceptor, Interceptor interface must be implemented. It has three methods:

  1. public void init() It is invoked only once and used to initialize the interceptor.
  2. public void intercept(ActionInvocation ai) It is invoked at each request, it is used to define the request processing logic. If it returns string, result page will be invoked, if it returns invoke() method of ActionInvocation interface, next interceptor or action will be invoked.
  3. public void destroy() It is invoked only once and used to destroy the interceptor.