Chapter 6: Validator


Reviewing the Controller Layer of the Mini HR Application

To solidify your understanding of the Controller layer of Struts applications, this section reviews the Controller layer of the Mini HR application developed in Chapter 2. Doing so clearly illustrates the core components involved in creating the Controller layer.

Mini HR's Controller layer consists of a single class: SearchAction. The SearchAction class, shown next, is responsible for processing requests from the search.jsp page:

package com.jamesholmes.minihr;     import java.util.ArrayList;     import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;     import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping;     public final class SearchAction extends Action {   public ActionForward execute(ActionMapping mapping,     ActionForm form,     HttpServletRequest request,     HttpServletResponse response)     throws Exception   {     EmployeeSearchService service = new EmployeeSearchService();     ArrayList results;         SearchForm searchForm = (SearchForm) form;         // Perform employee search based on what criteria was entered.     String name = searchForm.getName();     if (name != null && name.trim().length() > 0) {       results = service.searchByName(name);     } else {       results = service.searchBySsNum(searchForm.getSsNum().trim());     }         // Place search results in SearchForm for access by JSP.     searchForm.setResults(results);         // Forward control to this Action's input page.     return mapping.getInputForward();   } } 

When a search is initiated, the Struts ActionServlet Controller servlet delegates processing to this Action class. This class acts as the liaison between the Model layer and the View layer. Based on the search criteria entered from the View layer, SearchAction determines which search method to invoke on the EmployeeSearchService Model class and passes in the data from the View layer. EmployeeSearchService returns the results of the search and SearchAction manages getting the data back to the View layer.



Struts. The Complete Reference
Struts: The Complete Reference, 2nd Edition
ISBN: 0072263865
EAN: 2147483647
Year: 2004
Pages: 165
Authors: James Holmes

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net