Differences between revisions 3 and 4
Revision 3 as of 2007-02-21 02:12:52
Size: 3484
Editor: 64-160-69-34
Revision 4 as of 2009-09-20 23:12:46
Size: 3493
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.gif {{attachment:employee_list.gif}}
Line 86: Line 86:
[:StrutsQuickStart2:Next: handling events in Struts Action] [[StrutsQuickStart2|Next: handling events in Struts Action]]

Employee list with Struts + JSP

This example produces the same output as a servlet-based application, but now this is a Struts application.


In the servlet-based Model 2 example the name of the presentation page was hardcoded in the servlet. Would not it be nice to externalize this information? Struts framework allows to do that with the help of struts-config.xml file. Instead of a servlet you will be developing a custom Action class:

{{{import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import org.apache.struts.action.Action; import model.EmployeeManager;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.util.ArrayList;


  • This action prepares employees for display


public class EmployeesListAction extends Action {

  • /**
    • Handles incoming request
    • /

    public ActionForward execute(ActionMapping mapping,

    • // Load employee list and store it 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 "success" outcome to display employee list (see struts-config.xml). // "success" is not hardcoded, you can use any string you want. return mapping.findForward("success");

} }}}

Notice that execute method returns an action mapping, passing to it "render" outcome code. The correspondence of this code to a presentation page is defined in struts-config.xml file. This file plays the role of the traffic cop in a Struts application, it contains all configuration information and shows the structure of Struts application.


  • <action-mappings>

    • <action path = "/employeesList"

      • <forward name = "success" path = "/pages/employees.jsp"/>


    • ..


As you can see, the Action class knows nothing about what happens after it finishes. The "forward" element in the action mapping can be changed without changing the action class. Also, Action class does not forward to a specific location, instead it returns the outcome code. This makes Action class more abstract that traditional servlet.

The presentation page is unchanged from servlet/JSP example, here it is for completeness:

{{{<%@ page contentType="text/html;charset=UTF-8" language="java" %>


  • <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>${employee.id}</td> <td>${employee.name}</td> <td>${employee.salary}</td>






Next: handling events in Struts Action

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