Struts2 - Interceptors Introduction

Interceptors Introduction

Author: Ashish Garg

The struts2 framework makes it easy to share certain solutions using an "Interceptor" strategy. When you request a resource that maps to an "action", the framework invokes the Action object. But, before the Action is executed, the invocation can be intercepted by another object. After the Action executes, the invocation could be intercepted again. Unsurprisingly, we call these objects "Interceptors."

Interceptors can execute code before and after an Action is invoked. Most of the framework's core functionality is implemented as Interceptors. Features like double-submit guards, type conversion, object population, validation, file upload, page preparation, and more, are all implemented with the help of Interceptors. Each and every Interceptor is pluggable, so you can decide exactly which features an Action needs to support.

Interceptors can be configured on a per-action basis. Your own custom Interceptors can be mixed-and-matched with the Interceptors bundled with the framework. Interceptors "set the stage" for the Action classes, doing much of the "heavy lifting" before the Action executes

Strtus2 provide some default interceptor but developer can create his own interceptor as per requirement. Below is an configuration of interceptor in struts configuration file.


<package name="default" extends="struts-default">
<interceptors>
<interceptor name="timer" class=".."/>
<interceptor name="logger" class=".."/>
</interceptors>
<action name="login"
class="tutorial.Login">
<interceptor-ref name="timer"/>
<interceptor-ref name="logger"/>
<result name="input">login.jsp</result>
<result name="success"
type="redirectAction">/secure/home</result>
</action>
</package>

User Defined Interceptor



import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.StrutsStatics;

/**
*
* @author Ashish Garg
*/
public class LoginInterceptor extends AbstractInterceptor implements StrutsStatics {

private static final Log log = LogFactory.getLog(LoginInterceptor.class);
private static final String USER_HANDLE = "USER_ID";
private static final String LOGIN_ATTEMPT = "loginAttempt";

@Override
public void init() {
log.info("Intializing LoginInterceptor");
System.out.println("Intializing LoginInterceptor");
}

public String intercept(ActionInvocation invocation) throws Exception {

final ActionContext context = invocation.getInvocationContext();
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
HttpSession session = request.getSession(true);

// Is there a "user" object stored in the user's HttpSession?
Object user = session.getAttribute(USER_HANDLE);
if (user == null) {
// The user has not logged in yet.

// Is the user attempting to log in right now?
String loginAttempt = request.getParameter(LOGIN_ATTEMPT);

/* The user is attempting to log in. */
if (!StringUtils.isBlank(loginAttempt)) {
return invocation.invoke();
}
return "login";
} else {
return invocation.invoke();
}
}
}

Struts-Configuration For User Defined Login Interceptor


<struts>
<!-- Configuration for the default package. -->
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor class="com.Garg.Action.LoginInterceptor" name="loginInterceptor">
</interceptor>
<interceptor-stack name="loginStack">
<interceptor-ref name="loginInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="loginStack"></default-interceptor-ref>
<global-results>
<result name="login" type="redirect">Login</result>
</global-results>
<action name="Login" class="com.Garg.Action.LoginAction">
<interceptor-ref name="loginStack"></interceptor-ref>
<result name="success">/jsp/ViewJSP.jsp</result>
<result name="input">/jsp/Login.jsp</result>
</action>
</package>
</struts>

0 comments:

Post a Comment