Struts - SPLessons

Struts 2 Example

Chapter 8

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Struts 2 Example

Struts 2 Example

shape Example

Following is an Struts 2 Example which shows how to build an application by using Struts 2 and following are the steps which shows that where to place the XML files and how to run an application. While working on Struts 2 applications some precautions needs to followed such as struts.xml file should be placed under the source folder, in the struts configuration action class and result tags will be used. While using this tags in the project developer has know what is the functionality of every element in the configuration file, every frame work will have configuration files why because those files are hearts to the frameworks.

shape Step 1

Go to the File -> New -> select Dynamic web project.Enter the Project name and all the details, Click on Finish.

shape Step 2

Add the jar files in lib folder like Struts 2 jar file, Servlet-API.jar file and ojdbc14.jar.

shape Step 3

One need to create the index.jsp, after that the action class container will map the web.xml. In Struts2, action class is FilterDispatcher class, which contain the and Execute Filter package.

shape Step 4

The container will check the Action class in struts.xml file. Then, one can create the struts.xml and action class in src folder.

shape Step 5

After executing the execute() contain Action class, JSP pages display the result.

A simple example of Struts 2 application.

shape Example 2

In this Struts 2 Example, one employee details will be stored in the database from browser page using Struts 2 action class.

shape Step 1

Create the project directory structure.

shape Step 2

Create the index.jsp.


<%@ taglib uri="/struts-tags" prefix="s" %>
 <s:form action="employee">
   <s:textfield name="id" label="Employee Id"></s:textfield>
   <s:textfield name="name" label="Employee Name"></s:textfield>
   <s:textfield name="salary" label="Salary"></s:textfield>
   <s:submit value="save"></s:submit>

Here created all the text boxes to enter required details and also created the save button.

shape Step 3

Create the web.xml under WEB-INF folder.


<?xml version="1.0" encoding="UTF-8"?>

Here make sure that filter-name should be same.The FilterDispatcher (org.apache.struts2.dispatcher.FilterDispatcher) is used in the early Struts2 development, and it’s deprecated since Struts 2.1.3.
While using Struts version >= 2.1.3, it’s always recommended to upgrade the new filter class – StrutsPrepareAndExecuteFilter (

shape Step 4

Create the struts.xml in src folder.


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "">
  <package name="default" extends="struts-default">

   <action name="employee" class="com.itoolsinfo.Employee">
        <result name="success">success.jsp</result>

Where success is a predefined result type. action element is the sub component of package. It speaks to an activity to be conjured for the approaching request. It has name, class and method attributes. In the event that you don’t determine name property as a matter of course execute() technique will be summoned for the predetermined actiion class.

shape Step 5

Create the action class under project name/src/package name/Action class name.

package com.itoolsinfo;

public class Employee
  private int id;
  private String name;
  private double salary;
  public int getId()
   return id;
  public void setId(int id)
  { = id;
  public String getName() 
   return name;
  public void setName(String name) 
   { = name;
   public double getSalary() 
   return salary;
  public void setSalary(double salary)
   this.salary = salary;
  public String execute()
   return "success";

Here used Set and Get methods to work on data members.

shape Step 6

Create the success.jsp in WEB-INF folder.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<%@ page isELIgnored="false"%>
     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      ItoolsInfo Employee Details successfully saved.

         Employee Id : ${id}
         Employee Name : ${name}
         Employee Salary: ${salary}


The content type is nothing to identify that which data has to be sent.

shape Step 7

Run on the server.

shape Output

The output will be displayed in the browser as follow.Enter the all the details, click on save button.

When save the entered details message will be as follows.

shape Programming

  • Struts 2 Example – The struts.xml file should be placed under the src folder.
  • Struts 2 Example – The web.xml file should placed under WEB-INF folder.
  • The JSP, HTML files should be placed under WEB ROOT.


shape Key Points

  • All the required JAR files should be placed under lib folder.
  • Success is the struts predefined element.