Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Struts Wiki" for change 
notification.

The following page has been changed by MichaelJouravlev:
http://wiki.apache.org/struts/StrutsQuickStart2

------------------------------------------------------------------------------
  == 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. 
  
  inline:employee_list_delete.gif
  
- 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. To distinguish the "Delete" command from the request to merely 
display the list, we will add a special parameter to the link. We will define 
this parameter as an event for this action in the {{{struts-config.xml}}} file, 
and we will redesign the Action class to perform employee removal code when 
this parameter is received.
+ 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"
+         type  = "actions.EmployeesListAction"
+         parameter = "deleteEvent=delete">
+     <forward name = "render" path = "/pages/employees.jsp"/>
+     <forward name = "finished" path = "/employeesList.do" redirect = "true"/>
+ </action>}}}
+ 
- Here is modified EmployeeListAction class, notice that it extends 
EventDispatchAction class:
+ Now let us modify EmployeeListAction class by extending EventDispatchAction 
class:
+ 
- {{{public class EmployeesListActionDelete extends EventDispatchAction {
+ {{{public class EmployeesListAction extends EventDispatchAction {
  
      /**
       * Handles "deleteEvent" event (see struts-config.xml file). Deletes an 
employee
@@ -56, +75 @@

  }
  }}}
  
- Now the definition of "Delete" event in the action mapping in 
{{{struts-config.xml}}} file:
- {{{<action path  = "/employeesListDelete"
-         type  = "actions.EmployeesListActionDelete"
-         parameter = "deleteEvent=delete">
-     <forward name = "render" path = "/jspstruts2/employees.jsp"/>
-     <forward name = "finished" path = "/employeesListDelete.do" redirect = 
"true"/>
- </action>}}}
- 
- The "parameter" attribute states that if "deleteEvent" request parameter is 
received, then "delete" method of EmployeesListActionDelete should be called. 
Also notice two mappings, "render" and "finished". First is used to display 
employee list, second is used to finish processing of "deleteEvent" event and 
to redisplay the item list by redirecting to it.
- 
- Finally, the JSP. Notice the <html:link> tag to create a link, JSTL 2.0 is 
used for convenience:
+ 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" %>
  
  <html:html>
    <body>
+ 
      <table>
        <tr>
          <th align="left">Emp #</th>
@@ -89, +99 @@

          </tr>
        </c:forEach>
      </table>
+ 
    </body>
  </html:html>}}}
  

Reply via email to