Spring显示错误消息

时间:2015-01-05 09:23:10

标签: java spring exception

我使用的是弹簧4.1.3.RELEASE

我有以下表格:

<form method="GET">
    <label for="employeeId">Id:</label> 
    <form:input path="empId" />
    <input type="submit" />
</form>

提交按钮发送请求,该请求将通过以下方法处理:

@RequestMapping(value="/{empId}", method=RequestMethod.GET)
public String getEmployeeById(@PathVariable @ModelAttribute String empId, BindException binding, Model model){
    try{
        model.addAttribute("employee", employeeDAO.getEmployeeById(Integer.valueOf(empId)));
    } catch (EmptyResultDataAccessException ex) {
        binding.reject("405", "Employee not found");
    }
    return "employeesList";
}

employeeDAO.getEmployeeById方法会抛出EmptyResultDataAccessException,如果没有具有此类ID的员工。如果抛出异常,我需要显示错误消息。我提供的代码不起作用。它引发了一次爆发:

java.lang.IllegalStateException: An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments to which they apply: public java.lang.String com.badmitrii.EmployeeListController.getEmployeeById(java.lang.String,org.springframework.validation.BindException,org.springframework.ui.Model)
    org.springframework.web.method.annotation.ErrorsMethodArgumentResolver.resolveArgument(ErrorsMethodArgumentResolver.java:64)
    org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
    org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

我尝试删除@ModelAttribute,但发生了相同的删除。如何解决这个问题?也许有更好的方法来做到这一点,而不是试图处理BindingException类的异常?

1 个答案:

答案 0 :(得分:1)

根据例外情况,您应该删除@PathVariable,仅使用@ModelAttribute

因为根据例外,BindingResult只能在@RequestBody,@ModelAttribute. or @RequestPart之后添加,而且您在这里使用两个注释的组合,(@PathVariable && @ModelAttribute

有关详情,请参阅Source Code了解此错误。

PS: - 经过上述更改后,您可能会获得400.然后您将不得不从JSP更改empId发送方式。