The Artima Developer Community
Sponsored Link

Web Buzz Forum
HOW-TO: Creating a Struts 2 Interceptor

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Lucas Alberione

Posts: 6
Nickname: alberione
Registered: Oct, 2009

Lucas Alberione is Software Engineer based in Boston, MA
HOW-TO: Creating a Struts 2 Interceptor Posted: Dec 15, 2009 2:16 PM
Reply to this message Reply

This post originated from an RSS feed registered with Web Buzz by Lucas Alberione.
Original Post: HOW-TO: Creating a Struts 2 Interceptor
Feed Title: Techtulia
Feed URL: http://www.techtulia.com/feeds/posts/default
Feed Description: Shamelessly random thoughts on software happenings
Latest Web Buzz Posts
Latest Web Buzz Posts by Lucas Alberione
Latest Posts From Techtulia

Advertisement

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.

Prerequisites

This guide assumes the following:

Setting up a Struts 2 project in Eclipse

Please refer to my previous post for instructions.

Writing the 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.

Writing the custom interceptor class

Create a ActionNameInterceptor class in the sample.interceptors package. It must extend the com.opensymphony.xwork2.interceptor.AbstractInterceptor class. Make sure it looks like:


package sample.interceptors;

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

/**
* 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:

http://localhost:8080/Struts2CustomInterceptor/sampleAction.action

and

http://localhost:8080/Struts2CustomInterceptor/sampleAction2.action

Resources

Struts 2 architecture: http://struts.apache.org/2.x/docs/architecture.html
Struts 2 interceptors developer's guide: http://struts.apache.org/2.0.14/docs/interceptors.html
AbstractInterceptor class: http://struts.apache.org/2.0.6/struts2-core/apidocs/com/opensymphony/xwork2/interceptor/AbstractInterceptor.html
ActionInvocation class: http://struts.apache.org/2.0.6/struts2-core/apidocs/com/opensymphony/xwork2/ActionInvocation.html
ActionContext class: http://struts.apache.org/2.0.6/struts2-core/apidocs/com/opensymphony/xwork2/ActionContext.html

Read: HOW-TO: Creating a Struts 2 Interceptor

Topic: iClassic Turns Your iPhone (jailbroken) in to iPod Classic Previous Topic   Next Topic Topic: Easily Schedule or Execute Powershell Scripts on Windows 7 with PS1Exec

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use