Differences between revisions 2 and 3
Revision 2 as of 2007-02-21 00:42:19
Size: 6058
Comment:
Revision 3 as of 2009-09-20 23:12:46
Size: 6074
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
inline:employee_list_view.gif {{attachment:employee_list_view.gif}}
Line 7: Line 7:
inline:employee_view.gif {{attachment:employee_view.gif}}

Action interaction in Struts and form submission: view an employee

employee_list_view.gif

Let us change the application to view an employee information in a separate form.

employee_view.gif

To view an employee we will create a separate Action class, EmployeeActionView, which will handle "view" event:

{{{/**

  • Action that displays an employee in a separate HTML form.
  • Uses EventActionDispatcher to handle events like "view" and "close". */

public class EmployeeActionView extends Action {

  • /**************************************************************************
    • Set up event dispatching
    • ************************************************************************/
    /**

    protected ActionDispatcher dispatcher = new EventActionDispatcher(this); /**

    • Dispatch an event to an appropriate event handler. By using a dispatcher
    • an action class does not need to extend a DispatchAction or other

    • dispatching class flavor.
    • /

    public ActionForward execute(ActionMapping mapping,

    • return dispatcher.execute(mapping, form, request, response);
    } /**************************************************************************
    • Event handlers
    • ************************************************************************/
    /**
    • Handles "view" event (see struts-config.xml file). Loads an existing Employee
    • from the list for preview.
    • /

    public ActionForward view (ActionMapping mapping,

    • EmployeeForm empForm = (EmployeeForm) form; // Load existing item; ID of item to load is already in the form bean; // Struts has populated the form bean with request values.

      ActionMessages errors = loadEmployee(empForm);

      // Employee has been found and loaded successfully ==> view employee if (errors == null || errors.isEmpty()) {

      • empForm.setHeader("View employee"); return mapping.findForward("render");

      // Could not load employee ==> queue errors to the session object and redirect // to the employee list page. } else {

      • request.getSession().setAttribute(Globals.ERROR_KEY, errors); return mapping.findForward("finished");
      }
    } /**
    • Handles "close" event (see struts-config.xml file).
    • /

    public ActionForward close (ActionMapping mapping,

    • // Redirect to the employee list page return mapping.findForward("finished");
    } /**************************************************************************
    • Helpers
    • *************************************************************************/
    /**
    • Loads item from storage (in this example from the session object)
    • @return non-empty ActionMessages object if could not load item.

    • /

    private ActionMessages loadEmployee(EmployeeForm empForm) {

    • ActionErrors errors = new ActionErrors(); try {

      • String empId = empForm.getEmpId();

        Employee employee = EmployeeManager.loadEmployee(empId); if (employee != null) {

        • empForm.setName(employee.getName()); empForm.setSalary(String.valueOf(employee.getSalary())); return null;
        } else {
        • errors.add("ERROR", new ActionMessage("crud.itemnotfound", empId)); return errors;

        }
      } catch (Throwable e) {
      • e.printStackTrace();

        errors.add("ERROR", new ActionMessage("crud.internalstorageerror", e.getMessage())); return errors;

      }
    }

} }}}

We will change the employee list page so that EmployeeActionView is called when a user clicks on employee ID:

{{{<%@ 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>

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

        • <tr>

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

          </tr>

        </c:forEach>

      </table>

    </body>

</html:html>}}}

The employee list is displayed with the simple Action class as in the first Struts sample (see above). The configuration file reflects the relationship of two actions:

{{{<form-beans>

</form-beans>

<action path = "/employeesListView"

  • <forward name = "render" path = "/jspstruts3/employees.jsp"/>

</action>

<action path = "/employeeActionView"

  • type = "actions.EmployeeActionView" name = "crudform" scope = "request" parameter = "view,close">

  • <forward name = "render" path = "/jspstruts3/employee-view.jsp"/> <forward name = "finished" path = "/employeesListView.do" redirect = "true"/>

</action>}}}

This example uses EmployeeForm to temporarily store input/output data.

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