Srikanth Technologies

Getting started with Struts 2.0

In this blog, I show how to get started with Struts 2.0 with a simple application. I use NetBeans IDE 6.5 for this and Struts 2.0 plugin.

What is Struts 2.0?

Struts 2 is the next version of Struts 1.x. Unfortunately there are not many things that carried forward from Struts 1.x to Struts 2.0. A small application is the best way to understand the differences. I am writing this blog keeping on my mind readers who have already worked with Struts 1.x. However, Struts 2 depends more on Intercepting Filter pattern replacing Front controller (remember ActionServlet is based on Front Controller design pattern).

Getting started with Struts 2.0

In order to use Struts 2.0 in NetBeans IDE 6.5 follow the steps given below: You are ready to create a new web application with Struts 2 support.

Creating Web Application With Struts 2.0 support

Now, let us create a new web application that uses Struts 2. As we select Struts 2.0 support, all the required libraries and configuration files are automatically added to our project.
  1. Select File->New Project.
  2. Select Java Web under Categories and Web application under Projects.
  3. Give web application name and project location as you like.
  4. Select Apache Tomcat as Server. However, you can select any server you like.
  5. In Frameworks step, select Struts2 as the framework to use.
  6. Click on Finish.
NetBeans creates a project with required files. Though Not all of them are required for now, you need not disturb them. Let us focus on files that we have to create for our application.

Struts2 is based on Filters. The following web.xml shows how filter FilterDispatcher is configured by default. Netbeans IDE automatically places these entries when a new web application is created with Struts2 support.


Creating Interest Calculation application

We will create necessary files to take Principal Amount and Interest rate and from user and then display interest amount as the result of the process.


The first thing we want to create is a .jsp file to take input from user. Following code displays two textboxes to take input from user. Label attribute is used for prompt message. Attribute name specifies the property in the action class that is associated with the input field. So first textfield copies values into amount property and second textfield copies value into rate property.  Another important attribute is action attribute of form tag.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
 <%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

        <h2>Interest Calculation </h2>
        <s:form action="interest">
            <s:textfield  name="amount" label="Principal Amount "/>
            <s:textfield name="rate" label="Interest Rate "/>
            <s:submit value="Calculate"/>


Now we need to create action class to receive the data from input elements and do the required process. It contains execute() method, but it has no parameters (like in previous version). Create required properties - amount, rate and result. Property result is used to provide result to result.jsp (yet to create it).

Place InterestAction class in action package as follows.

package action;

public class InterestAction {

    private double amount, rate, result;

    public double getResult() {
        return result;

    public double getAmount() {
        return amount;

    public void setAmount(double amount) {
        this.amount = amount;

    public double getRate() {
        return rate;

    public void setRate(double rate) {
        this.rate = rate;

    public InterestAction() {

    public String execute() throws Exception {
        result = amount * rate / 100;
        return "success";



Modify struts.xml, which is in default package under source packages. In Struts 2, xml file providing details of action class is placed in classpath.

Action tag specifies the name of the class, action name and also where to go based on the value returned by execute() method of InterestAction class. In this case we want to go to result.jsp when execute() method returns success.

Create a package that extends struts-default package so that all the required functionality is inherited. Place action tag in package tag as shown in the following code.

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    <package name="default" extends="struts-default">
        <action name="interest"  class="action.InterestAction">
            <result name="success">/result.jsp</result>


Result.jsp is used to display the result after business logic is done. Business logic is placed in execute() method of InterestAction class. Using property tag we retrieve value from  result property of InterestAction class.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
 <%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

        <h2>Interest  = <s:property value="result" /> </h2>

That's all you have to do. Now deploy the web application and run interest.jsp.

You will see two textboxes with prompts on the left. Enter amount and interest rate and click on Calculate button. You should see interest displayed in result.jsp page.

Post Your Comment

Enter the code given in the above image :
Enter Your Name :
Enter Your Email Address :
Enter Your Comment :


Posted By srinivasu On 30-Nov-09 12:38:41 PM

I am one of the regular user of this website.This url is really good to under the basic stuff of struts 2.0. Sir, if possile could you please provide a list of points, of basic differences between 1.x and 2.0 versions in terms of framework features. thx.

Posted By madhusudhan On 30-Nov-09 03:54:31 PM

sir, i am one of your student sir .. on comparing to the struts 1, here there no representation of actionform with the validate method, without this how we will validate the input values .. and also tell me sir how the input values(form) are submitted to the getter- setter methods

Posted By madhusudhan On 30-Nov-09 04:51:57 PM

sir ,, now i got the new project on springs sir,, i would i approach sir... actually some part is of struts

Posted By madhusudhan On 02-Dec-09 11:42:33 AM

sir, im using netbeans 6.7.1 struts2 plugin is not processing

Posted By Zubin Shah On 11-Dec-09 05:05:53 PM

It really helps me a lot....Thanks

Posted By visitor On 09-Feb-10 05:34:10 PM

Madhusudhan !
Download the new plug-in from version 1.2 for org-netbeans-modules-web-frameworks-struts2.nbm and org-netbeans-modules-web-frameworks-struts2lib20011.nbm. it will work!!
Visitor of this site!!
Add 2003 to my email id (reducing spam)

Posted By Kishore On 10-Apr-10 01:07:54 PM

This Example is really good and very easy to understand for a beginner.

Posted By sakthivel On 28-May-10 04:05:30 PM

dear sir iam working in one it company we are using netbeans 6.7,here i can download struts2.1 org-netbeans-modules-web-frameworks-struts2.nbm and org-netbeans-modules-web-frameworks-struts2lib20011.nbm. but it will not work in netbeans6.7,so please help for me

Posted By Pirabu G On 03-Jun-10 04:27:28 PM

thankyou. it is very useful.

Posted By Anjali On 24-Jul-10 10:45:29 AM

Hello sir,

I am using netbeans 6.9. i dono how to run struts in this ide.i searching in net .but i wont get the proper knowledge. please help me sir.

Posted By anonymous On 06-Aug-10 11:05:17 PM

Great Article!
I am a newbie with struts framework and I was wondering if you can place the struts2 configuration, properties, *-validation.xml files etc out of the WEB-INF directory and some other directory of my choice?
Will really appreciate any response.

Posted By Vishal S. Pawar On 21-May-11 11:10:37 PM

Hello Sir,
I am a regular viewer of your site, The examples which you have gives are very useful and simple to clear doubts.Would you please add some more examples related to the database transactions. Thanks,

Posted By Venkat On 22-Jul-11 06:53:54 PM

Excellent. Very nicely explained the basics of struts 2.0 . Thankyou.

Posted By Nazim Uddin On 18-Oct-11 11:49:43 AM

sir, thank you for your publishing. i want to know how i will get data from database using struts 2 framework. is it possible? if yes, mail me how.
thank you.

Posted By sripada On 05-Nov-11 12:47:24 PM


Posted By Ravindra Singh On 02-Jan-12 04:10:47 PM

If you could show the directory structure that would be great !!

Posted By kavita On 08-Jun-12 12:15:07 PM

It is a very easy and good example.Sir can u plz give me one example on validation.I m waiting for that. plz give that example as soon as posible.

Posted By Jayabalaji D On 28-Dec-16 06:35:12 PM

ya it's doing gud.