Custom Interceptors example in webwork and struts2

xwork.xml

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.1.dtd">
<xwork>
<include file="webwork-default.xml"/>
<package name="default" extends="webwork-default">

<interceptors>
<interceptor name="greeting" class="com.hello.GreetingInterceptor">
</interceptor>

<interceptor name="nextinterceptor" class="com.hello.NextInterceptor" />
<interceptor name="thirdinterceptor" class="com.hello.ThirdInterceptor" />
<interceptor name="stack1interceptor" class="com.hello.Stack1Interceptor" />
<interceptor name="stack2interceptor" class="com.hello.Stack2Interceptor" />
<interceptor name="beforeafterinterceptor" class="com.hello.BeforeAfterInterceptor" />

<interceptor-stack name="TestStack">
<interceptor-ref name="stack1interceptor"/>
<interceptor-ref name="stack2interceptor" />
<interceptor-ref name="stack2interceptor"/>
</interceptor-stack>

</interceptors>

<action name="hello" class="com.hello.HelloAction" method="execute">
<interceptor-ref name="greeting"/>
<result name="success">/success.jsp</result>
</action>

<action name="stack" class="com.hello.StackAction" method="execute">
<result name="success">/stacksuccess.jsp</result>
<interceptor-ref name="TestStack" />
</action>

<action name="beforeafter" class="com.hello.BeforeAfterAction" method="execute">
<result name="success">/beforeafter.jsp</result>
<interceptor-ref name="beforeafterinterceptor" />
</action>

</package>
</xwork>

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<filter>
<filter-name>webwork</filter-name>
<filter-class>com.opensymphony.webwork.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>webwork</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

<taglib>
<taglib-uri>webwork</taglib-uri>
<taglib-location>/WEB-INF/lib/webwork-2.2.2.jar</taglib-location>
</taglib>
</web-app>

GreetingInterceptor.java

package com.hello;

import java.util.Map;

import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.Interceptor;

public class GreetingInterceptor implements Interceptor {

public void init() {
}

public void destroy() {
}

public String intercept(ActionInvocation invocation) throws Exception {
String greeting = "Hello World Interceptor4444";

invocation.getInvocationContext().getSession().put("hello", greeting);

String result = invocation.invoke();

return result;
}

}

HelloAction.java

package com.hello;

import com.opensymphony.xwork.ActionSupport;

public class HelloAction extends ActionSupport {

public String execute() {

System.out.println("Action Calling");

return "success";

}
}

StackAction.java

package com.hello;

import com.opensymphony.xwork.ActionSupport;

public class StackAction extends ActionSupport {

public String execute() {

System.out.println("Stack Action Calling");

return SUCCESS;

}
}

Stack1Interceptor.java

package com.hello;

import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.Interceptor;

public class Stack1Interceptor implements Interceptor{
String test="hello";

public String getTest() {
return test;
}

public void setTest(String test) {
this.test = test;
}

public void destroy() {

}

public void init() {

}

public String intercept(ActionInvocation invocation) throws Exception {

String stack1="Stack1 Interceptor" ;

invocation.getInvocationContext().getSession().put("stack1", stack1);

System.out.println("Stack interceptor1 --------- "+stack1+" --- ");

String result = invocation.invoke();

System.out.println("After Invocation Stack interceptor1 ---------");

return result;
}

}

Stack2Interceptor.java

package com.hello;

import javax.servlet.http.HttpServletRequest;

import com.opensymphony.webwork.interceptor.SessionAware;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.Interceptor;

public class Stack2Interceptor implements Interceptor{

public void destroy() {

}

public void init() {

}

public String intercept(ActionInvocation invocation) throws Exception {

String stack2="Stack2 Interceptor" ;

invocation.getInvocationContext().getSession().put("stack2", stack2);

System.out.println("Stack interceptor2 --------- "+stack2+" --- ");

String result = invocation.invoke();

System.out.println("After Invocation Stack interceptor2 ---------");

return result;
}

}
BeforeAfterInterceptor.java

package com.hello;

import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.AroundInterceptor;

public class BeforeAfterInterceptor extends AroundInterceptor
{
protected void after(ActionInvocation invoke, String arg1) throws Exception {
System.out.println("After Invocation BeforeAfterInterceptor");
}

protected void before(ActionInvocation invoke) throws Exception {
System.out.println("Before Invocation BeforeAfterInterceptor");

}

}

BeforeAfterAction.java

package com.hello;

import com.opensymphony.xwork.Action;

public class BeforeAfterAction implements Action{

public String execute() throws Exception {

System.out.println("come to Before After Action");
return SUCCESS;
}

}

NextInterceptor.java

package com.hello;

import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.Interceptor;

public class NextInterceptor implements Interceptor{

public void destroy() {

}

public void init() {

}

public String intercept(ActionInvocation invocation) throws Exception {

String greeting1="Next Interceptor" ;

String first=(String) invocation.getInvocationContext().getSession().get("greeting");
System.out.println("Getting session valueeeee --- "+first);

invocation.getInvocationContext().getSession().put("next", greeting1);

System.out.println("Test interceptor2 --------- "+greeting1+" --- ");

String result = invocation.invoke();
//System.out.println("the resullt isssss 222222222"+result);
return result;
}

}

beforeafter.jsp

<hr/>
<h2>Success</h2>

index.jsp

<%@ taglib uri="webwork" prefix="ww" %>

<hr/>
<center>
<a href="hello.action">Click Here</a><br/><br/>
<a href="stack.action">Click Here To Test Stack</a>
<br/><br/>
<a href="beforeafter.action">Click Here To Test Before After Action</a>
</center>
<hr/>

stacksuccess.jsp

<%@ taglib uri="webwork" prefix="ww" %>

<!--
<b>Stack Hello</b> <br/>
<b>
<ww:text name="${#session.stack1}"></ww:text>
</b><br/>

<b>
<ww:text name="${#session.stack2}"></ww:text>
</b><br/>-->

success.jsp

<%@ taglib uri="webwork" prefix="ww" %>

<b>Hello</b> <br/>

<b>
<ww:text name="${#session.greeting}"></ww:text>
</b><br/>

<b>
<ww:text name="${#session.next}"></ww:text>
</b><br/>

<b>
<ww:text name="${#session.third}"></ww:text>
</b>

<b>
<ww:text name="${#session.greeting2}"></ww:text>
</b>

No comments: