You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

See the Interceptors page for an overview of how interceptors work.

Interceptor interface

Interceptors must implement the com.opensymphony.xwork2.interceptor.Interceptor interface.

Interceptor.java
public interface Interceptor extends Serializable {

    void destroy();

    void init();

    String intercept(ActionInvocation invocation) throws Exception;
}

The init method is called the after interceptor is instantiated and before calling intercept. This is the place to allocate any resources used by the interceptor.

The intercept method is where the interceptor code is written. Just like an action method, intercept returns a result used by Struts to forward the request to another web resource. Calling invoke on the parameter of type ActionInvocation will execute the action (if this is the last interceptor on the stack) or another interceptor.

Overwrite destroy to release resources.

AbstractInterceptor

The AbstractInterceptor class provides an empty implementation of init and destroy, and can be used if these methods are not going to be implemented.

Mapping

TODO

Example

Assuming there is an action of type "MyAction", with a setDate(Date) method, this simple interceptor will set the date of the action to the current date:

Interceptor Example
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class SimpleInterceptor extends AbstractInterceptor {

    public String intercept(ActionInvocation invocation) throws Exception {
       MyAction action = (MyAction)invocation.getAction();
       action.setDate(new Date());
       return invocation.invoke();
    }
}
  • No labels