Employee list with Struts + JSP

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

employee_list.gif

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;

/**

public class EmployeesListAction extends Action {

} }}}

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.

{{{<struts-config>

<struts-config>}}}

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

<html>

</html>}}}

Next: handling events in Struts Action

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