Tuesday, December 15, 2009

How to configure Global/Default Interceptor in Struts 2

Global/Default interceptor means to me like the interceptor would be applied to the entire action classes may span accross mutiple packages.

Step 1: Create an interceptor
Step 2: Register the interceptor in interceptors tag.
Step 3: Create an interceptor stack having all the necessary references to the interceptors
Step 4: If there are multiple packages are involved then put the interceptor in the common package and
all other packages would extend it.

For Example:

<struts>
<package name="base-package" extends="struts-default">

<interceptors>
<!-- interceptor used to initialize resource used through out the application-->
<interceptor name="ResourceInit" class="com.test.ResourceInitInterceptor"/>

<!-- interceptor stack composes struts provided interceptors and custody related-->
<interceptor-stack name="base-default-stack">
<interceptor-ref name="ResourceInit"/>
<!-- "defaultStack" defined in struts in struts-default.xml-->
<interceptor-ref name="defaultStack"/>
</interceptor-stack>

</interceptors>

<!-- Default interceptor executed for the entire actions -->
<default-interceptor-ref name="base-default-stack"/>
</package>

<include file="/com/struts_config/struts_module_1.xml" />
<include file="/com/struts_config/struts_module_2.xml" />

</struts>

Note: struts_module_1 and struts_module_2 must extends "base-package"

No comments:

Post a Comment