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

New page:
== 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. 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.

Here is modified EmployeeListAction class, notice that it extends 
EventDispatchAction class:
{{{public class EmployeesListActionDelete extends EventDispatchAction {

    /**
     * Handles "deleteEvent" event (see struts-config.xml file). Deletes an 
employee
     * from the list of employees.
    public ActionForward delete (ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception 
{

        // 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) {
            boolean deleted = EmployeeManager.deleteEmployee(empId);
            if (!deleted) {
                ActionErrors errors = new ActionErrors();
                errors.add("ERROR", new ActionMessage("crud.itemnotfound", 
empId));
                saveErrors(request.getSession(), errors);
            }
        }
        // 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,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws 
Exception {
        // 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");
    }
}
}}}

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:

{{{<%@ 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>
        <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>
        </tr>
      </c:forEach>
    </table>
  </body>
</html:html>}}}

Reply via email to