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/StrutsQuickStart1

New page:
== Employee list with Struts + JSP ==

In the previous Model 2 example the name of the presentation page is hardcoded 
in the servlet. Would not it be nice to externalize this information? Struts 
framework allows to do that with {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;

/**
 * Accesses the virtual imployee database.
 */
public class EmployeesListAction extends Action {

    /**
     * Handles incoming request
     */
    public ActionForward execute(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");
    }
}
}}}

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:

{{{<struts-config>
    <action-mappings>
      <action path  = "/employeesList" type  = "actions.EmployeesListAction">
          <forward name = "render" path = "/jspstruts1/employees.jsp"/>
      </action>
    ...
<struts-config>}}}

The presentation page is unchanged from previous Servlet/JSP example. 

Reply via email to