Sample Example
package com.googleforjava.interceptor;
import java.util.Iterator;
import java.util.Map;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class TestActionInterceptor implements Interceptor {
/**
*
*/
private static final long serialVersionUID = 1L;
public void destroy() {
System.out.println("CustomInterceptor destroy() is called...");
}
public void init() {
System.out.println("CustomInterceptor init() is called...");
}
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("CustomInterceptor, before invocation.invoke()...");
Map params = actionInvocation.getInvocationContext().getParameters();
// Getting all request parameters from jsp page on which you have called any
// action
System.out.println(params.size());
Iterator itr = params.entrySet().iterator();
while(itr.hasNext()) {
Object element = itr.next();
System.out.print(element + " ");
Map.Entry pairs = (Map.Entry)itr.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}
System.out.println("Stack :"+actionInvocation.getStack().size());
System.out.println("Stack :"+actionInvocation.getStack().VALUE_STACK);
params = actionInvocation.getStack().getContext();
itr = params.entrySet().iterator();
while(itr.hasNext()) {
Object element = itr.next();
System.out.print(" --> "+element + " ");
Map.Entry pairs = (Map.Entry)itr.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}
System.out.println(" Action Name : "+actionInvocation.getAction().toString());
String result = actionInvocation.invoke();
System.out.println(" Result : "+ result);
System.out.println("CustomInterceptor, after invocation.invoke()...");
return result;
}
}
Struts2 Interceptor Read Parameter in interceptor class
in struts.xml file
in one common package.
<interceptors>
<interceptor name="crunchPrepInterceptor" class="com.googleforjava.thinksharp.interceptorsPrep.GatePass" />
<interceptor-stack name="thinksharpPrepStack">
<interceptor-ref name="thinksharpPrepInterceptor"/>
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="thinksharpPrepStack"></default-interceptor-ref>
in interceptor class:
HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(StrutsStatics.HTTP_RESPONSE);
Map sessionMap = actionInvocation.getInvocationContext().getSession();
String className = actionInvocation.getAction().getClass().getName();
System.out.println("Stack :"+actionInvocation.getStack().size());
System.out.println("Stack :"+actionInvocation.getStack().VALUE_STACK);
HttpServletRequest request = ServletActionContext.getRequest();
System.out.println(" Action Name : "+actionInvocation.getAction().toString());
LoginUserBO loginUserBO=(LoginUserBO) sessionMap.get("user");
System.out.println("\n\n\n\n B4 Invocation Interceptor \n Class Name is: "+className);
if(loginUserBO==null){
System.out.println("\n\n User Object is null ");
}else{
userDetails(loginUserBO);
}
String result=actionInvocation.invoke();
sessionMap = actionInvocation.getInvocationContext().getSession();
loginUserBO=(LoginUserBO) sessionMap.get("user");
System.out.println("\n\n\n After Invocation Interceptor Result is: "+result);
if(loginUserBO==null){
System.out.println("\n\n User Object is null ");
}else{
userDetails(loginUserBO);
}
return result;
}
private void userDetails(LoginUserBO loginUserBO)
{
System.out.println("\n\n User Name is: "+loginUserBO.getFirst_name()+" User Role Id is :"+loginUserBO.getRole_id());
}
No comments:
Post a Comment