eclipse Spring 将空 ModelAndView 返回给 DispatcherServlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29906597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:35:33  来源:igfitidea点击:

Spring returning null ModelAndView to DispatcherServlet

javaeclipsespringspring-mvc

提问by CodeMed

I am trying to create a minimal reproduction of a spring mvc project for use in diagnosing errors in more complex projects. But when I try to click on the url that triggers a controller method, the eclipse console gives an error indicating that the ModelAndView is null. While this other questionasks about a similar error, you can see that the question was not answered because four separate people gave +1 to the comment that the answer needs to be elaborated upon. I also looked at this other posting, but following it's advice of using org.springframework.web.servlet.ModelAndViewinstead of Map did not resolve the problem. Also, I would like to use Map if possible, because Map works in my more complex apps. How can I resolve this error?

我正在尝试创建 spring mvc 项目的最小复制,用于诊断更复杂项目中的错误。但是当我尝试点击触发控制器方法的 url 时,eclipse 控制台给出一个错误,表明 ModelAndView 为空。虽然this other question询问了类似的错误,但您可以看到该问题没有得到回答,因为四个不同的人对需要详细说明答案的评论给予了+1。我还查看了其他帖子,但遵循使用org.springframework.web.servlet.ModelAndView而不是 Map的建议并没有解决问题。另外,如果可能的话,我想使用 Map,因为 Map 可以在我更复杂的应用程序中使用。 我该如何解决这个错误?

Here is the controller method being called when the error is thrown:

这是抛出错误时调用的控制器方法:

@RequestMapping(value = "/TriggerTheError", method = RequestMethod.GET)
public String processFindForm(HttpServletRequest request, BindingResult result, Map<String, Object> model) {        
    return "ReproduceError";
}

The console prints out the following log when the above-controller method is called:

当上面的控制器方法被调用时,控制台打印出以下日志:

13:59:10.874 [http-nio-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'MinimalDbaseExample' processing GET request for [/MinimalDbaseExample/TriggerTheError]
13:59:10.874 [http-nio-8080-exec-3] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /TriggerTheError
13:59:10.874 [http-nio-8080-exec-3] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/TriggerTheError]
13:59:10.875 [http-nio-8080-exec-3] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Matching patterns for request [/TriggerTheError] are [/**]
13:59:10.875 [http-nio-8080-exec-3] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - URI Template variables for request [/TriggerTheError] are {}
13:59:10.875 [http-nio-8080-exec-3] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Mapping [/TriggerTheError] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@186a461c] and 1 interceptor
13:59:10.875 [http-nio-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - Last-Modified value for [/MinimalDbaseExample/TriggerTheError] is: -1
13:59:10.875 [http-nio-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'MinimalDbaseExample': assuming HandlerAdapter completed request handling
13:59:10.875 [http-nio-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request


Answer

回答



The solution was to simply remove the BindingResult because it was not needed in this situation. By removing the BindingResult from the arguments to the controller method, the error was removed. Later on, if BindingResult is required, the solution then would simply be to add the other required elements for BindingResult which are not required for this test case.

解决方案是简单地删除 BindingResult,因为在这种情况下不需要它。通过从控制器方法的参数中移除 BindingResult,错误被移除。稍后,如果需要 BindingResult,那么解决方案就是为 BindingResult 添加其他所需的元素,而这些元素对于此测试用例是不需要的。

I will mark the other user's answer as accepted. However, the preceding paragraph is the solution that resolved the problem.

我会将其他用户的答案标记为已接受。但是,上一段是解决问题的解决方案。

回答by ninj

If you meant to return the string ReproduceError, then mark the return value with @ResponseBody.

如果您打算返回字符串ReproduceError,则用 标记返回值@ResponseBody

On the other hand, if you've got a View called ReproduceErrorthen perhaps your view resolver isn't configured properly?

另一方面,如果您调用了一个视图,ReproduceError那么您的视图解析器可能没有正确配置?

EDIT: ok, looks like a rendering of the ReproduceErrorview is intended, but from the logfile it seems that the controller method isn't being hit.

编辑:好的,看起来像是ReproduceError打算渲染视图,但是从日志文件中似乎没有命中控制器方法。