Wednesday, November 9, 2011

Interceptors with Struts 2


Well another mile stone to me and learning all by yourself is such a pain. Now I understand the interceptors better. After all I was having a lot of issues with them

An interceptor as the name signifies gets triggered when an action is invoked provided the action is associated with the interceptor

 First of all following is my action class

import com.opensymphony.xwork2.ActionSupport;

public class TestLoggerAction extends ActionSupport{

       public String execute()    {
      
              System.out.println("new Inside Action");
              return SUCCESS;
             
       }
}


Following is a my interceptor class. I have imported Action support class here as well in order to overrule the return value of the action. You would understand the same when you uncomment a specific code indicated in the following block of code

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class MyLoggingInterceptor extends ActionSupport implements Interceptor  {

       private static final long serialVersionUID = 1L;


       public String intercept(ActionInvocation invocation) throws Exception {
      
             
              System.out.println("Finishing execution stack for action //TestLogger");

                     //----------  Un comment this line and you will see the result of the prevention of action
                     //----------  from being executed

                     //if(true) return ERROR;  


                     //----------------------------------------------------------------------------------

              String result = invocation.invoke();
              // Post processing
      
              System.out.println("Finishing execution stack for action //TestLogger");
              return result;
       }

          //called during interceptor destruction
              public void destroy() {
                     System.out.println("CustomInterceptor2 destroy() is called...");
              }
        
              //called during interceptor initialization
              public void init() {
                     System.out.println("CustomInterceptor2 init() is called...");
              }

}

Finally my struts file.

<?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>
       <package name="default" extends="struts-default">

              <interceptors>
                     <interceptor name="logger"
                           class="MyLoggingInterceptor">
                     </interceptor>

                     <interceptor-stack name="loggingStack">
                           <interceptor-ref name="logger" />
                           <interceptor-ref name="defaultStack" />
                     </interceptor-stack>

              </interceptors>

              <action name="TestLogger" class=" TestLoggerAction">
                     <interceptor-ref name="loggingStack" />
                     <result name="error">/InterceptorFailure.jsp</result>
                     <result name="success">/InterceptorSuccess.jsp</result>
              </action>

       </package>
</struts>
             
Be very very precise with the characters you type in. Case sensitivity is hell lot of issue for me. L

You will get the following page if you un comment the block of code I have indicated in the interceptor class



And the following line page if you proceed as such



And your console would contain the following info


Hope this helps  :)

No comments:

Post a Comment