Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struts 2 interceptor that runs after the page executes?

I'm using Struts 2. Using an interceptor, I create a database connection at the start of each page execution.

So for example, if a user goes to "myAction.do", it will create the database connection and then call myAction.do method.

What I'm looking for now is an interceptor or any other way to automatically call a method after the page execution, which will close the database connection.

Is that possible?

like image 286
Ali Avatar asked May 10 '13 04:05

Ali


People also ask

What are the interceptors in struts?

Interceptor is an object that is invoked at the preprocessing and postprocessing of a request. In Struts 2, interceptor is used to perform operations such as validation, exception handling, internationalization, displaying intermediate result etc.

Which interceptor stack is are available in Struts2 bundle interceptors?

Struts 2 comes with a set of pre defined interceptors and interceptor stacks which you can use out of the box.

How do you use parameters interceptor in struts?

The params interceptor also known as parameters interceptor is used to set all parameters on the valuestack. It is found in the default stack bydefault. So you don't need to specify it explicitely.

What is action invocation?

An ActionInvocation represents the execution state of an Action . It holds the Interceptors and the Action instance. By repeated re-entrant execution of the invoke() method, initially by the ActionProxy , then by the Interceptors, the Interceptors are all executed, and then the Action and the Result .


2 Answers

In interceptor you can write pre processing and post processing logics.

Pre processing logic will execute before the action executes and post processing logic executes after the action executes.

Struts2 provides very powerful mechanism of controlling a request using Interceptors. Interceptors are responsible for most of the request processing. They are invoked by the controller before and after invoking action, thus they sits between the controller and action. Interceptors performs tasks such as Logging, Validation, File Upload, Double-submit guard etc.

Whatever you will write after invocation.invoke(); that will execute after executes action

SEE HERE FOR EXAMPLE

like image 108
PSR Avatar answered Oct 11 '22 00:10

PSR


Fully described in http://blog.agilelogicsolutions.com/2011/05/struts-2-interceptors-before-between.html

You can have interceptor:

  1. Before Action
  2. Between Action and Result
  3. After View Rendered

As mentioned in the site here are code samples

Before Interceptor

public class BeforeInterceptor extends AbstractInterceptor {
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
      // do something before invoke
       doSomeLogic();
      // invocation continue    
      return invocation.invoke();
    }
  }
}

Between action and result

public class BetweenActionAndResultInterceptor extends AbstractInterceptor {
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
      // Register a PreResultListener and implement the beforeReslut method
      invocation.addPreResultListener(new PreResultListener() { 
        @Override
        public void beforeResult(ActionInvocation invocation, String resultCode) {
          Object o = invocation.getAction();
          try{
            if(o instanceof MyAction){
              ((MyAction) o).someLogicAfterActionBeforeView();
            }
            //or someLogicBeforeView()
          }catch(Exception e){
            invocation.setResultCode("error");
          }
        }
      });

      // Invocation Continue
      return invocation.invoke();
    }
  }
}

After View Rendered

public class AfterViewRenderedInterceptor extends AbstractInterceptor {
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
      // do something before invoke
      try{
        // invocation continue    
        return invocation.invoke();
      }catch(Exception e){
        // You cannot change the result code here though, such as:
        // return "error"; 
        // This line will not work because view is already generated  
        doSomeLogicAfterView();
      } 
    }
  }
}
like image 30
Alireza Fattahi Avatar answered Oct 11 '22 01:10

Alireza Fattahi