简体   繁体   English

弹簧显示错误消息

[英]Spring display error message

I'm using spring 4.1.3.RELEASE 我正在使用Spring 4.1.3.RELEASE

I have the following form: 我有以下表格:

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

The submit button send a request which is going to be handled by the following method: 提交按钮发送请求,该请求将通过以下方法处理:

@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";
}

The employeeDAO.getEmployeeById method throws the EmptyResultDataAccessException if there's no an employee with such an id. 如果没有这样的员工,那么employeeDAO.getEmployeeById方法将引发EmptyResultDataAccessException I need to display an error message if the exception was thrown. 如果引发了异常,我需要显示一条错误消息。 The code I provided doesn't work. 我提供的代码无效。 It throws an expcetion: 它抛出一个异常:

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)

I tried to remove @ModelAttribute , but the same expcetion was occured. 我试图删除@ModelAttribute ,但是发生了相同的异常。 How to resolve the issue? 该如何解决? Maybe there's a better way to do that rather than trying to handle an exception with the BindingException class? 也许有比这样做更好的方法,而不是尝试使用BindingException类处理异常?

As per the exception detail ,you should remove @PathVariable ,use only @ModelAttribute . 根据异常的详细信息,您应该删除@PathVariable ,仅@ModelAttribute

Because as per exception the BindingResult can only be added after @RequestBody,@ModelAttribute. or @RequestPart 因为按照BindingResult只能在@RequestBody,@ModelAttribute. or @RequestPart之后添加@RequestBody,@ModelAttribute. or @RequestPart @RequestBody,@ModelAttribute. or @RequestPart ,and here you are using combination of two annotation,( @PathVariable && @ModelAttribute ) @RequestBody,@ModelAttribute. or @RequestPart ,在这里您使用两个注解的组合,( @PathVariable && @ModelAttribute

For more info:- see Source Code for this error. 有关更多信息:-请参阅源代码以获取此错误。

PS:- After above change,you may be getting 400.Then you will have to change the empId sending way from JSP. PS:-经过上述更改,您可能会得到400。然后,您将不得不更改JSP的empId发送方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM