sábado, 3 de agosto de 2019

SpringMVC Form Handling y Spring Backing Bean

SpringMVC Form Handling y Spring Backing Bean

https://github.com/jalbertomr/SpringMVCsimple/commit/e3ab9d18e395507a67075a690e99bfd7fd51d84c


En este caso, si hay error en los datos de entrada, se limpian los campos y se recarga la página de captura de empleado.

El código original sin backing Bean

AddEmployeeController.java
package com.bext.empManSys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.bext.empManSys.domain.Employee;
import com.bext.empManSys.service.EmployeeService;

@Controllerpublic class AddEmployeeController {

   @Autowired   private EmployeeService employeeService;

   @RequestMapping(value = "/altaEmpleado", method = RequestMethod.GET)
   public ModelAndView show() {
      return new ModelAndView("altaEmpleado", "emp", new Employee());
   }

   @RequestMapping(value = "/altaEmpleado", method = RequestMethod.POST)
   public ModelAndView addEmployee(
         @RequestParam("empId")String empId,
         @RequestParam("nombre")String nombre,
         @RequestParam("designacion")String designacion,
         @RequestParam("sueldo")String sueldo) {
      double sueldoDouble = new Double(sueldo);
      Employee empleado = new Employee(empId, nombre, designacion, sueldoDouble);
      employeeService.addNewEmployee(empleado);
      return new ModelAndView("/altaEmpleadoOK","empleado", empleado);
   }
}

Con backing bean

AddEmployeeController.java
package com.bext.empManSys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.bext.empManSys.domain.Employee;
import com.bext.empManSys.service.EmployeeService;

@Controllerpublic class AddEmployeeController {

   @Autowired   private EmployeeService employeeService;
   
   @RequestMapping(value = "/altaEmpleado", method = RequestMethod.GET)
   public ModelAndView show() {
      return new ModelAndView("altaEmpleado", "empleado", new Employee());
   }
   
   @RequestMapping(value = "/altaEmpleado", method = RequestMethod.POST)
   public ModelAndView processRequest( Employee empleado, Errors result) { 
      if (result.hasErrors()) {
         return new ModelAndView("/altaEmpleado", "empleado", empleado);
      }
      employeeService.addNewEmployee(empleado);
      return new ModelAndView("/altaEmpleadoOK","empleado", empleado);
   }
}


eot

No hay comentarios:

Publicar un comentario