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.

Installations:

  • 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(){
       
       //ServletActionContext.get
        Employee emp1 = new Employee();
            emp1.setFirstName("jayaram");
            emp1.setLastName("p");
            emp1.setId(1001L);
       
       Employee emp2 = new Employee();
            emp2.setFirstName("emp2");
            emp2.setLastName("p");
            emp2.setId(1002L);
       
       Employee emp3 = new Employee();
           emp3.setFirstName("emp3");
           emp3.setLastName("p");
           emp3.setId(1003L);
       
       Employee emp4 = new Employee();
          emp4.setFirstName("emp4");
          emp4.setLastName("p");
          emp4.setId(1004L);
       
          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 Employee.java 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) {
            this.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>
       <div>
       <s:a href="addEmployeView.action" cssClass="btn btn-primary">Add Employee</s:a>
       </div>
       <s:if test="list.size > 0">
       <table class="table table-striped">
       <thead>
       <tr>
       <td>Employee Id</td>
       <td>First Name</td>
       <td>Last Name</td>
      </tr>
       </thead>
       <s:iterator value="list">
       <tr>
       <td><s:property value="id" /> </td>
       <td><s:property value="firstName" /></td>
       <td><s:property value="lastName" /></td>
       </tr>
       </s:iterator>
       </table>
       </s:if>
       </div>
  • 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.

result

 

Download Struts 2 Tutorial

Leave a Comment

Your email address will not be published. Required fields are marked *