How to do it...

For @Controllers to execute some DAO transactions and business-related logic, add the service layer by following these steps:

  1. After ensuring that the DAO layer is ready for use, add two more packages to contain our service interfaces and implementation. All interfaces must be in org.packt.dissect.mvc.service and their implementation must be saved inside org.packt.dissect.mvc.service.impl.
  2. Create the following interfaces that will be used to fetch the service implementations at the controller layer. These will just expose read and add record transactions:
public interface DepartmentService { 
  public List<Department> readDepartments(); 
  public void addDepartment(Department dept); 
} 
 
public interface EmployeeService { 
  public List<Employee> readEmployees(); 
  public void addEmployeee(Employee emp); 
}
  1. Then, create the service implementations of the interfaces in Step 2. The other purpose of these services is to add more logic on the records being retrieved, or manipulate some details before adding, viewing, and deleting them as shown here:
@Service 
public class DepartmentServiceImpl implements  
    DepartmentService { 
  
  @Autowired 
  private DepartmentDao departmentDaoImpl; 
 
  @Override 
  public List<Department> readDepartments() { 
        return departmentDaoImpl.getDepartments(); 
  } 
 
  @Override 
  public void addDepartment(DepartmentForm dept) { 
 
    Department deptData = new Department(); 
    deptData.setDeptId(dept.getDeptId()); 
    deptData.setName(dept.getName()); 
    departmentDaoImpl.addDepartmentBySJI(deptData); 
  } 
} 
 
@Service 
public class EmployeeServiceImpl implements  
      EmployeeService { 
  
  @Autowired 
  private EmployeeDao employeeDaoImpl; 
 
  @Override 
  public List<Employee> readEmployees() { 
        return employeeDaoImpl.getEmployees(); 
  } 
 
  @Override 
  public void addEmployeee(EmployeeForm empForm) {  
    Employee emp = new Employee(); 
    emp.setDeptId(empForm.getEmpId()); 
    emp.setFirstName(empForm.getFirstName()); 
    emp.setLastName(empForm.getLastName()); 
    emp.setAge(empForm.getAge()); 
    emp.setBirthday(empForm.getBirthday()); 
    emp.setEmail(empForm.getEmail()); 
    emp.setDeptId(empForm.getDeptId()); 
    employeeDaoImpl.addEmployeeBySJI(emp); 
  } 
} 

All service implementations must be injected into the container through the annotation @Service. Although this annotation classifies a class to be a service, it has no special background event yet when compared to @Repository.

The service layer manages the form models (such as EmployeeForm) because there are model attributes containing all the request data after form submission. DAO layer manages, on the other hand, the data models (such as Employee) created by the service layer.
  1. Afterwards, create a controller called DepartmentController that will complete the whole MVC application with database connection in the background. This controller will invoke a form asking for department information needed to be saved into the hrs database schema, and from there will generate a view for all the records:
@Controller 
public class DepartmentController { 
   
  @Autowired 
  private DepartmentService departmentServiceImpl; 
   @RequestMapping("/deptform.html") 
  public String initForm(Model model){ 
 
    DepartmentForm departmentForm = new DepartmentForm(); 
    model.addAttribute("departmentForm", departmentForm); 
     return "dept_form";  
  } 
  
  @RequestMapping(value="/deptform.html",  
    method=RequestMethod.POST) 
  public String submitForm(Model model,  
    @ModelAttribute("departmentForm") 
         DepartmentForm departmentForm){ 
 
    departmentServiceImpl.addDepartment(departmentForm); 
    model.addAttribute("departments", 
     departmentServiceImpl.readDepartments()); 
    return "dept_result"; 
  } 
}
  1. Create the views indicated by the @Controller and update the views.properties and message_en_US.properties.
  2. Save all files. Then clean, install, and deploy the project.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.249.210