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:

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"


Now let us modify EmployeeListAction class by extending EventDispatchAction class:

{{{public class EmployeesListAction extends EventDispatchAction {

} }}}

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" %>



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