0
votes
When any exception occured in catch block then how can we show the error message in the same jsp i.e employee.jsp and how to jsp should look like?
am getting this error message in the console;what is the meaning of this error?
ERROR: org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Failed to invoke @ExceptionHandler method: public org.springframework.web.servlet.ModelAndView com.kesava.tutorial.controller.HomeController.addEmployee(com.kesava.tutorial.dto.EmployeeDTO,org.springframework.validation.BindingResult) throws com.kesava.tutorial.util.SpringUtilException
java.lang.IllegalStateException: No suitable resolver for argument [0] [type=com.kesava.tutorial.dto.EmployeeDTO]
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
    @ExceptionHandler({ SpringUtilException.class })
    public ModelAndView addEmployee(@Valid EmployeeDTO employeeDTO,
            BindingResult result) throws SpringUtilException {
        ModelAndView mav = new ModelAndView("employee");
        if (result.hasErrors()) {
            List employees = employeeDao.getAllEmployees();
            return new ModelAndView("employee", "employeeList", employees);
        } else {
            BeanUtils.copyProperties(employeeDTO, employees);
            System.out.println("addEmployee employees! " + employees);
            try {
                employeeDao.persist(employees);
            } catch (EntityExistsException e) {
                throw new SpringUtilException(e, "Employee Duplicate");
            } catch (Exception ex) {
                throw new SpringUtilException(ex, "Failed to add Employee");
            }
            // after inserting show the employees
            List employees1 = employeeDao.getAllEmployees();


            mav.addObject("isDataSaved" , "Data saved Successfully");
            mav.addObject("employeeList" , employees1);

            return mav;

        }
        }

2

2 Answers

0
votes

If you want to see the error within the JSP output, do not use System.out.println. Instead, add the exception stack trace to a model object you print out in your JSP.

Something like:

try { 
   employeeDao.persist(employees); 
} 
catch (EntityExistsException e) { 
   model.addAttribute("error", e.getMessage()); 
}

That way you can see the exception in the JSP, and print it. You can access it by the attribute key, like this: ${error}

0
votes

Did you debug the code. What the error means spring could not invoke the method in the controller it is failing due to binding.

1.Just read it carefully it says illegal state exception which cannot be handled by the exception handler since you are handling only SpringUtilException

2.And secondly There is not a suitable resolver for the argument check your httprequest and verify all the parameters are passed correctly the mapping failure usually in most cases will be date format mismatch or the data type mismatch