Tuesday, August 6, 2013

Struts 2 Interceptors Tutorial

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.
1.<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.
01.<!DOCTYPE struts PUBLIC
02."-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04. 
05.<struts>
06.<package name="default" extends="struts-default">
07.<interceptors>
08.<interceptor-stack name="exampleStack">
09.<interceptor-ref name="exception" />
10.<interceptor-ref name="alias" />
11.<interceptor-ref name="servletConfig" />
12.<interceptor-ref name="prepare" />
13.<interceptor-ref name="i18n" />
14.<interceptor-ref name="chain" />
15.<interceptor-ref name="debugging" />
16.<interceptor-ref name="profiling" />
17.<interceptor-ref name="scopedModelDriven" />
18.<interceptor-ref name="modelDriven" />
19.<interceptor-ref name="fileUpload" />
20.<interceptor-ref name="checkbox" />
21.<interceptor-ref name="staticParams" />
22.<interceptor-ref name="actionMappingParams" />
23.<interceptor-ref name="params">
24.<param name="excludeParams"> dojo\..*,^struts\..*</param>
25.</interceptor-ref>
26.<interceptor-ref name="conversionError" />
27.<interceptor-ref name="validation">
28.<param name="excludeMethods">populate</param>
29.</interceptor-ref>
30.<interceptor-ref name="workflow">
31.<param name="excludeMethods"> input,back,cancel,browse</param>
32.</interceptor-ref>
33.</interceptor-stack>
34.</interceptors>
35.<action name="*Sample" method="{1}" class="vaannila.SampleAction">
36.<interceptor-ref name="exampleStack" />
37.<result name="populate">/first.jsp</result>
38.<result name="success">/success.jsp</result>
39.</action>
40.</package>
41.</struts>
If you see our exampleStack the only change that we have done is, we have changed theexcludeMethods 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.
01.<!DOCTYPE struts PUBLIC
02."-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04. 
05.<struts>
06.<package name="default" extends="struts-default">
07.<action name="*Sample" method="{1}" class="vaannila.SampleAction">
08.<interceptor-ref name="defaultStack" >
09.<param name="validation.excludeMethods"> populate</param>
10.</interceptor-ref>
11.<result name="populate">/first.jsp</result>
12.<result name="success">/success.jsp</result>
13.</action>
14.</package>
15.</struts>
Our SampleAction class contains the following code.
01.package vaannila;
02. 
03.import com.opensymphony.xwork2.ActionSupport;
04. 
05.public class SampleAction extends ActionSupport{
06. 
07.private static final long serialVersionUID = 1L;
08. 
09.public void validate()
10.{
11.System.out.println("validate() method called");
12.}
13. 
14.public String populate()
15.{
16.System.out.println("populate() method called");
17.return "populate";
18.}
19. 
20.public String execute()
21.{
22.System.out.println("execute() method called");
23.return SUCCESS;
24.}
25.}
When you run the code using the defaultStack without any changes. The following messages gets printed in the console.
1.validate() method called
2.populate() method called
3.validate() method called
4.execute() method called
When you run the code the using the exampleStack we just created. The follwing messages gets printed in the console.
1.populate() method called
2.validate() method called
3.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.

No comments:

Post a Comment