Meyyappan has posted 101 posts at DZone. View Full User Profile

Struts 2 Interceptors Tutorial

06.15.2012
| 40398 views |
  • submit to reddit

In this tutorial you will see different ways to create you own interceptor stack and associate it with the action class.

Struts 2 comes with a set of pre defined interceptors and interceptor stacks which you can use out of the box. The struts-default.xml file contains the struts-default package which defines all the interceptors and the interceptor stacks. You can use the stack that meets your need.

When you extend your package from the struts-default package by default the defaultStack will be used for all the actions in your package. This is configured in the struts-default.xml file in the following way.

<default-interceptor-ref name="defaultStack"/>

Let's now create our own interceptor stack. The interceptor-stack element is used to create an interceptor stack. A stack contains a group of interceptors. Each interceptor in the stack is defined using the interceptor-ref element. In this example we will create a stack similar to the defaultStack and customise the validation interceptor according to our need.

We have three methods in our SampleAction class, populate() ,execute() and validate(). Since we extend our class from ActionSupport which inturn implements the Validateable interface, the validate() method of the action class will be called by the workflow interceptor. By default the validate() method will be called during the execution of both populate() and execute() methods but we need to validate only when the execute() method is invoked.

We do this by specifying the populate method in the excludeMethods parameter of the validation interceptor.

The struts.xml file contains the following code.

<!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-stack name="exampleStack">
                    <interceptor-ref name="exception" />
                    <interceptor-ref name="alias" />
                    <interceptor-ref name="servletConfig" />
                    <interceptor-ref name="prepare" />
                    <interceptor-ref name="i18n" />
                    <interceptor-ref name="chain" />
                    <interceptor-ref name="debugging" />
                    <interceptor-ref name="profiling" />
                    <interceptor-ref name="scopedModelDriven" />
                    <interceptor-ref name="modelDriven" />
                    <interceptor-ref name="fileUpload" />
                    <interceptor-ref name="checkbox" />
                    <interceptor-ref name="staticParams" />
                    <interceptor-ref name="actionMappingParams" />
                    <interceptor-ref name="params">
                            <param name="excludeParams"> dojo\..*,^struts\..*</param>
                    </interceptor-ref>
                    <interceptor-ref name="conversionError" />
                    <interceptor-ref name="validation">
                            <param name="excludeMethods">populate</param>
                    </interceptor-ref>
                    <interceptor-ref name="workflow">
                            <param name="excludeMethods"> input,back,cancel,browse</param>
                    </interceptor-ref>
            </interceptor-stack>
        </interceptors>
        <action name="*Sample" method="{1}" class="vaannila.SampleAction">
            <interceptor-ref name="exampleStack" />
            <result name="populate">/first.jsp</result>
            <result name="success">/success.jsp</result>
        </action>
    </package>
</struts>

If you see our exampleStack the only change that we have done is, we have changed the excludeMethods of the validation interceptor, rest all is similar to the defaultStack. This is just to show you how to create your own interceptor stack, you can also achieve the same in a much simpler way.

You can extend your stack from the defaultStack and override the excludeMethods parameter of the validation interceptor in the following way to achieve the same result.

<!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">
        <action name="*Sample" method="{1}" class="vaannila.SampleAction">
            <interceptor-ref name="defaultStack" >
                <param name="validation.excludeMethods"> populate</param>
            </interceptor-ref>
            <result name="populate">/first.jsp</result>
            <result name="success">/success.jsp</result>
        </action>
    </package>
</struts>

Our SampleAction class contains the following code.

package vaannila;

import com.opensymphony.xwork2.ActionSupport;

public class SampleAction extends ActionSupport{

    private static final long serialVersionUID = 1L;
    
    public void validate()
    {
    	System.out.println("validate() method called");
    }
    
    public String populate()
    {
    System.out.println("populate() method called");
    	return "populate";
    }
    
    public String execute()
    {
        System.out.println("execute() method called");
        return SUCCESS;
    }
}

When you run the code using the defaultStack without any changes. The following messages gets printed in the console.

validate() method called
populate() method called
validate() method called
execute() method called

When you run the code the using the exampleStack we just created. The follwing messages gets printed in the console.

populate() method called
validate() method called
execute() method called

As you can see the validate() method is not invoked during populate. In this way you can customise the stack base on your requirement.

You can download this example by clicking the Download link below.

Source :Download
Source + Lib :Download
Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Debasish Panda replied on Sat, 2012/07/28 - 7:41am

Hello Sir,

 I really want to say thanks.Because Before read this tutorial I was doing Programme,but i was not able to understand the concept of Interceptor.To whom I was asking they are unknown about Struts2 .So,now after read this i am some how in comfortable position.Please carry on and clarify our doubts.

 

Thanks,

Debasish 

Pradeep Yadav replied on Sat, 2013/06/08 - 1:32pm

 really greate tutorial

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.