Differences between revisions 3 and 4
Revision 3 as of 2007-02-21 02:33:00
Size: 5424
Revision 4 as of 2009-09-20 23:12:46
Size: 5432
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
inline:employee_list_delete.gif {{attachment:employee_list_delete.gif}}

Event handling in Struts: delete an employee

Now after you got the idea how Struts fits the servlet/JSP environment, let us improve our application. How about being able to delete an employee from the list? To do this we will change the JSP page, adding an "Operation" column and creating a "Delete" link for each table row. The link would contain employee ID. When clicked, the link will be processed by the same Action that displays the list.


There are two different approaches to processing the "Delete" link: either to create a separate action class and refer to this action from the link, or to handle employee removal in the same action that displays action list. First approach is more traditional, while the second allows to have less action classes and less mappings in the config file, it is used in this guide.

To process different requests, an Action class should be able to distinguish between different request types. In case of this application, there are two request types:

  • First request type does not have any query parameters and means "show me the employee list".
  • Second request type has a special request parameter that means "delete an employee with ID passed along in this request".

We will use "deleteEvent" query parameter as a command to delete an employee. This parameter will be added to "Delete" links. We will define this parameter as an event in the struts-config.xml file, and we will redesign the Action class to perform employee removal when this parameter is received.

Let us start with the configuration file. Action events are defined in "parameter" attribute of an action mapping as "key=value" pairs. In this example, when "deleteEvent" parameter is present in the request, Struts will run delete method on the EmployeesListAction class.

{{{<action path = "/employeesList"

  • <forward name = "render" path = "/pages/employees.jsp"/> <forward name = "finished" path = "/employeesList.do" redirect = "true"/>


Now let us modify EmployeeListAction class by extending EventDispatchAction class:

{{{public class EmployeesListAction extends EventDispatchAction {

  • /**
    • Handles "deleteEvent" event (see struts-config.xml file). Deletes an employee
    • from the list of employees.

    public ActionForward delete (ActionMapping mapping,

    • // Reads empId parameter from the request. This parameter is part of the "Delete" link // in the employee table. If deletion failed, an error message is generated and is // queued into request scope, where it is pulled from by a JSP page. String empId = request.getParameter("empId"); if (empId != null) { } // Processing of the event has finished, do whatever is appropriate next, // which is to redisplay the updated employee list. return mapping.findForward("finished");
    } /**
    • Handles cases when no event is found in the request. In this case simply
    • displays the list of employees. Compare with EmployeeListAction.execute method.

    public ActionForward unspecified(ActionMapping mapping,

    • // Lookup virtual database, if not found - instantiate it // and store in the session under "employees" name

      HttpSession session = request.getSession(); ArrayList employees = (ArrayList) session.getAttribute("employees"); if (employees == null) {

      • employees = EmployeeManager.loadEmployees(); session.setAttribute("employees", employees);

      } // Return "render" outcome to display employee list (see struts-config.xml) return mapping.findForward("render");

} }}}

Finally, the JSP. Notice the <html:link> tag to create a link, JSTL 2.0 is used to create link parameters:

{{{<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>


  • <body>

    • <table>

      • <tr>

        • <th align="left">Emp #</th> <th align="left">Name</th> <th align="left">Salary</th> <th align="left">Operation</th>

        </tr> <c:forEach var="employee" items="${employees}">

        • <tr>

          • <td>${employee.id}</td> <td>${employee.name}</td> <td>${employee.salary}</td> <td><html:link action="employeesListDelete.do?deleteEvent&empId=${employee.id}">Delete</html:link></td>






StrutsQuickStart2 (last edited 2009-09-20 23:12:46 by localhost)