如何在spring mvc 3.0中的同一个jsp中显示运行时错误消息或sql错误消息

时间:2012-06-19 12:54:29

标签: spring-mvc

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 个答案:

答案 0 :(得分:0)

如果要在JSP输出中查看错误,请不要使用System.out.println。而是将异常堆栈跟踪添加到您在JSP中打印的模型对象。

类似的东西:

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

这样你可以在JSP中看到异常并打印它。您可以通过属性键访问它,如下所示: ${error}

答案 1 :(得分:0)

您是否调试了代码。错误表示弹簧无法在由于绑定而失败的控制器中调用该方法。

1。请仔细阅读它说非法状态异常,该异常状态异常无法由异常处理程序处理,因为您仅处理SpringUtilException

2。其次,该参数没有合适的解析器,请检查您的httprequest并验证所有参数是否正确传递,通常在大多数情况下,映射失败是日期格式不匹配或数据类型不匹配