Struts2 tutorial

Struts2 Tutorial

Struts2 tutorial is quick start for developers who want to learn struts. In this tutorial,  we will see how to setup Struts 2 application in Eclipse.  Struts 2  is MVC framework which is combination of Webframe work and Struts1.


  • Eclipse
  • Tomcat
  • Struts2

Struts2 Example:

  • In Eclipse, create a sample dynamic web application. File -> New -> Project -> Web -> Dynamic Web Application and give the project name.
  • Create the project structure shown in below.s1
  • Struts2 work on Filters to identify the actions paths. so just configure filter in web.xml. Where as other MVC frameworks requires Servlets need to configure in web.xml with URL mapping, which are responsible for dispatching request to controller.
  • Lets configure FilterDispatcher like below in web.xml :web config
  • In the step we will configure controllers and views. We need to create struts.xml file in src/main/resources folder which  holds all controllers and views.
    • package – package represents  module of the application. In general, struts application will be divided into multiple modules. by default, package extends “structs-default” which provides lot of in build interceptors.
    •  action  :  In this element,  you are required to configure below:
      • name:  This is URL Path name
      • class :  You need to provide action class
      • method:  method to execute for given URL path
      • result: you need to return JSP page struts sample
  • Create EmployeeAction class in org.javasavvy.demp package like below:
    • package org.javasavvy.demo;
      import java.util.ArrayList;
      import java.util.List;
      import org.apache.struts2.ServletActionContext;
      import org.javasavvy.demo.vo.Employee;
      import com.opensymphony.xwork2.Action;
      import com.opensymphony.xwork2.ActionContext;
      public class EmployeeAction {
       private Employee employe;
       private List<Employee> list;
       public String getEmployeeList(){
        Employee emp1 = new Employee();
       Employee emp2 = new Employee();
       Employee emp3 = new Employee();
       Employee emp4 = new Employee();
          list = new ArrayList<Employee>();
          list.add(emp1);  list.add(emp2); list.add(emp3); list.add(emp4);
       return Action.SUCCESS;
        public String addEmployeeView(){
           return Action.SUCCESS;
       public String addEmployee(){
            return Action.SUCCESS;
       public String deleteEmployee(){
            return Action.SUCCESS;
       public List<Employee> getList() {
            return list;
       public void setList(List<Employee> list) {
           this.list = list;
       public Employee getEmploye() {
          return employe;
       public void setEmploye(Employee employe) {
       this.employe = employe;
  • Create POJO in the package : org.javasavvy.demo.vo like below:
    • package org.javasavvy.demo.vo;
      public class Employee {
       private long id;
       private String firstName;
       private String lastName;
       public long getId() {
            return id;
       public void setId(long id) {
   = id;
       public String getFirstName() {
           return firstName;
       public void setFirstName(String firstName) {
           this.firstName = firstName;
       public String getLastName() {
       return lastName;
       public void setLastName(String lastName) {
       this.lastName = lastName;
  • Create employeeList.jsp  code
    • <div class="container">
       <h2>Employee List </h2>
       <s:a href="addEmployeView.action" cssClass="btn btn-primary">Add Employee</s:a>
       <s:if test="list.size > 0">
       <table class="table table-striped">
       <td>Employee Id</td>
       <td>First Name</td>
       <td>Last Name</td>
       <s:iterator value="list">
       <td><s:property value="id" /> </td>
       <td><s:property value="firstName" /></td>
       <td><s:property value="lastName" /></td>
  • Download the struts2 jar files and  copy to WEB-INF/lib folder. Deploy the application inn tomcat and access the application on below URL and below page will be returned.
  • URL :    http://localhost:9080/javasavvy-struts/employeeList.action.



Download Struts 2 Tutorial