Chapter 4: The View Layer

Reviewing the Model Layer of the Mini HR Application

Because Struts has little to do with the Model layer, there is little more to say about it. However, before moving on, it will be helpful to review the Model layer of the Mini HR application developed in Chapter 2. Doing so clearly illustrates how the Model code is separate from the rest of the application.

Mini HR's Model layer consists of two classes: EmployeeSearchService and Employee. The EmployeeSearchService class is shown next:

package com.jamesholmes.minihr;     import java.util.ArrayList;     public class EmployeeSearchService {   /* Hard-coded sample data. Normally this would come from a real data      source such as a database. */   private static Employee[] employees =   {     new Employee("Bob Davidson", "123-45-6789"),     new Employee("Mary Williams", "987-65-4321"),     new Employee("Jim Smith", "111-11-1111"),     new Employee("Beverly Harris", "222-22-2222"),     new Employee("Thomas Frank", "333-33-3333"),     new Employee("Jim Davidson", "444-44-4444")   };       // Search for employees by name.   public ArrayList searchByName(String name) {     ArrayList resultList = new ArrayList();         for (int i = 0; i < employees.length; i++) {       if(employees[i].getName().toUpperCase().indexOf(name.toUpperCase())          != -1)       {         resultList.add(employees[i]);       }     }          return resultList;   }       // Search for employee by social security number.   public ArrayList searchBySsNum(String ssNum) {     ArrayList resultList = new ArrayList();         for (int i = 0; i < employees.length; i++) {       if (employees[i].getSsNum().equals(ssNum)) {         resultList.add(employees[i]);       }     }         return resultList;   } }

EmployeeSearchService fulfills all three of the model's sublayers: external interface, business logic, and data access. The external interface is defined by the methods searchByName( ) and searchBySsNum( ). The business logic is contained in the implementation to those methods, which finds an employee based on either his or her name or social security number. Data access occurs each time the hard-coded Employee array is used.

In a small, sample application such as Mini HR, there is nothing wrong with implementing the entire Model within EmployeeSearchService. However, in a more complicated application, a class such as this would normally be used as only the external interface to the Model. In this approach, it would house only skeletal searchByName( ) and searchBySsNum( ) methods, which would pass through (or delegate) requests to the business logic sublayer where their actual implementation would exist. For example, the business logic sublayer code could be implemented in a class named EmployeeSearchImpl. Furthermore, EmployeeSearchImpl would then communicate with data access sublayer classes to actually query employee data from a database or such, rather than containing the data access code itself.

The Employee class, shown next, is a domain object used to represent an entity in the model. In this scenario it is used as a conduit for transferring data to and from the Model. As such, it can be thought of as being part of the Model layer.

package com.jamesholmes.minihr;     public class Employee {   private String name;   private String ssNum;       public Employee(String name, String ssNum) { = name;     this.ssNum = ssNum;   }       public void setName(String name) { = name;   }       public String getName() {     return name;   }       public void setSsNum(String ssNum) {     this.ssNum = ssNum;   }       public String getSsNum() {     return ssNum;   } }

Typically, an object of this type is referred to as a Data Transfer Object (DTO) or Value Object (VO) and is part of the external interface sublayer. The Model uses DTOs to send data back through its external interface and to accept data through its external interface. You can think of these classes as interfaces themselves because they specify the format and packaging of the data expected by the Model.

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

Similar book on Amazon
Struts 2 in Action
Struts 2 in Action
Murach's Java Servlets and JSP, 2nd Edition
Murach's Java Servlets and JSP, 2nd Edition
Jakarta Struts For Dummies
Jakarta Struts For Dummies
Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam
Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam © 2008-2017.
If you may any questions please contact us: