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.
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.
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.