Interceptors can execute code before and after an Action is invoked .Interceptors sits in between the controller and the action ,and invoked by controller before and after invoking an action. It can perform actions such as Logging,Validation,File upload etc.
In Struts 2 , interceptors are called one after the other in a configured order for each invocation of an action class. The interceptors have an opportunity to interact with the input from user, and to interact with the action after it completes it’s processing.
The main method in an interceptor is the intercept() method. This method is passed an ActionInvocation object, which contains the action, and the context for the action. The intercept method returns a String that maps to a “result” in struts.xml file.
Let’s understand the use of interceptor with an example. In this example we are checking whether the user is already logged in or not before going to the action class . Assume that first time when a user logs in we are storing the user details in session attribute called user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.j2eereference.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class SessionCheckInterceptor implements Interceptor { private static final long serialVersionUID = 1L; @Override public String intercept(ActionInvocation invocation) throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(true); // Checking whether the user Object that had set at login time is null or not if (session.getAttribute("user") == null) { return "invalidsession"; } return invocation.invoke(); // If user already logged in } @Override public void destroy() { System.out.println("Destroying Interceptor"); } @Override public void init() { System.out.println("Initialising Interceptor"); } } |
Now lets see the configuration in struts.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> ----- --- <interceptors> <interceptor name="SessionCheck" class="com.action.SessionCheckInterceptor"> </interceptor> <interceptor-stack name="newStack"> <interceptor-ref name="SessionCheck"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <!—-If we need to apply the interceptor for all the action classes--> <default-interceptor-ref-name="checkSessionStack"/><global-results> <result name="invalidSession">/login.jsp</result></global-results> <!-- If we need to apply interceptor for a particular action --> <action name="initAction" class="com.action.LoginAction" method="init"> <interceptor-ref name="newStack"> </interceptor-ref> <result name="invalidsession">/login.jsp</result> <result name="success">/welcome.jsp</result> </action> --- --- </struts> |
Leave a Reply