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.


    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    

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.

interest.jsp

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"
   "http://www.w3.org/TR/html4/loose.dtd">

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

    </body>
</html>

InterestAction.java

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";
    }

}    

struts.xml

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.


<!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="interest"  class="action.InterestAction">
            <result name="success">/result.jsp</result>
        </action>
    </package>
</struts>

result.jsp

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"
   "http://www.w3.org/TR/html4/loose.dtd">

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

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.