DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Culture and Methodologies
  3. Agile
  4. Struts 2 Interceptors Tutorial

Struts 2 Interceptors Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · Tutorial
Like (0)
Save
Tweet
Share
69.53K Views

Join the DZone community and get the full member experience.

Join For Free

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
Console (video game CLI) Download Element Execution (computing) Requirement workflow Links Interface (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Cucumber.js Tutorial With Examples For Selenium JavaScript
  • Apache Kafka Is NOT Real Real-Time Data Streaming!
  • GitLab vs Jenkins: Which Is the Best CI/CD Tool?
  • How To Build an Effective CI/CD Pipeline

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: