Wednesday, 13 April 2016

What are interceptors in Struts2 ?



Interceptors
  • allow custom code into the call stack
  • Much of the core functionality of the framework is implemented as Interceptors
  • Custom Interceptors are easy to add

Example 1. Logging interceptor
public class LoggingInterceptor implements Interceptor {
   public String intercept(ActionInvocation invocation) 
                 throws Exception {
        logMessage(invocation, START_MESSAGE);
        String result = invocation.invoke();
        logMessage(invocation, FINISH_MESSAGE);
return result;
}
}

Example 2. Timer interceptor
public class TimerInterceptor implements Interceptor {
   public String intercept(ActionInvocation invocation) 
                 throws Exception {
      if (log.isInfoEnabled()) {
         long startTime = System.currentTimeMillis();
         String result = invocation.invoke();
         long executionTime =System.currentTimeMillis() - startTime;
         ...
      } 
   }
}


Other Interceptors

Setting Parameters
  • ParameterInterceptor
  • StaticParameterInterceptor
  • ChainingInterceptor
  • ConversionErrorInterceptor
  • FileUploadInterceptor

Defining Workflow
  • DefaultWorkflowInterceptor
  • PrepareInterceptor
  • ServletConfigInterceptor
  • ExecuteAndWaitInterceptor

Preventing duplicate posts
  • Two types of token interceptors

No comments:

Post a Comment

Note: only a member of this blog may post a comment.