Here's a guide that explains how to write a simple Struts 2 custom interceptor. This interceptor simply injects the action name (defined in struts.xml) into actions that implement the sample.interceptors.ActionNameAware interface.
Create a ActionNameAware interface in the sample.interceptors package. Make sure it looks like:
package sample.interceptors;
/** * Interface for action name injection. */ public interface ActionNameAware {
/** * Setter for action name injection. */ public void setActionName( String actionName ); }
The sample.interceptors.ActionNameAware interface will allow the custom interceptor to inject (via the setActionName method) the action name into the action.
/** * Simple interceptor that injects action name into actions that implement the * sample.interceptors.ActionNameAware interface. */ public class ActionNameInterceptor extends AbstractInterceptor {
@Override public String intercept( ActionInvocation actionInvocation ) throws Exception {
//If the action implements the ActionAware interface... if ( actionInvocation.getAction() instanceof ActionNameAware ) {
//Cast the action to ActionNameAware ActionNameAware action = ( ActionNameAware ) actionInvocation.getAction();
//Inject the action name into action action.setActionName( actionInvocation.getInvocationContext().getName() ); }
//Delegate to next interceptor in interceptor stack return actionInvocation.invoke(); } }
Writing the action-name-aware action class
Create a Sample class in the sample.actions package. This class must implement the sample.interceptors.ActionNameAware interface you just wrote. Make sure it looks like:
package sample.actions;
import sample.interceptors.ActionNameAware;
/** * Simple action to show the use of the * sample.interceptors.ActionNameAware interface */ public class SampleAction implements ActionNameAware {
private String actionName;
public String execute(){ return "success"; }
/** * Action name getter. */ public String getActionName(){ return actionName; }
/** * Action name setter. * @see sample.interceptors.ActionNameAware#setActionName(java.lang.String) */ public void setActionName(String actionName) { this.actionName = actionName; } }
Writing a simple JSP to display the action name injected by the custom interceptor
Under /WEB-INF/pages create a file called actionName.jsp that looks like:
<%@ taglib prefix="s" uri="/struts-tags"%>
<html> <head> <title>Struts 2 Custom Interceptor Example</title> </head> <body> <h3> The action's name is <span style="color: red;"><s:property value="actionName"/></span> </h3> </body> </html>
Plugging the custom interceptor into Struts 2
Edit struts.xml so it looks like:
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="main" extends="struts-default" >
<interceptors>
<!-- Register the action name custom interceptor under the "actionNameInterceptor" name --> <interceptor name="actionNameInterceptor" class="sample.interceptors.ActionNameInterceptor" />
<!-- Create a new stack that extends Struts 2's default stack and includes the actionNameInterceptor custom interceptor --> <interceptor-stack name="myCustomStack"> <interceptor-ref name="defaultStack"/> <interceptor-ref name="actionNameInterceptor"/> </interceptor-stack>
</interceptors>
<!-- Use the "myCustomStack" custom interceptor stack by default --> <default-interceptor-ref name="myCustomStack"></default-interceptor-ref>
<!-- Define an action and name it "sampleAction" --> <action name="sampleAction" class="sample.actions.SampleAction"> <result name="success">/WEB-INF/pages/actionName.jsp</result> </action>
<!-- Define an action and name it "sampleAction2" --> <action name="sampleAction2" class="sample.actions.SampleAction"> <result name="success">/WEB-INF/pages/actionName.jsp</result> </action>
</package>
</struts>
Running the application
From Eclipse, start Tomcat and access the application by browsing to: