我正在尝试创建 spring mvc 项目的最小复制品,以用于诊断更复杂项目中的错误。但是,当我尝试单击触发 Controller 方法的 url 时,Eclipse 控制台会给出错误,指示 ModelAndView 为 null。而this other question询问类似的错误,您可以看到问题没有得到回答,因为有四个不同的人对需要详细说明答案的评论给出了+1。我还看了this other posting ,但遵循使用 org.springframework.web.servlet.ModelAndView 而不是 Map 的建议并没有解决问题。另外,如果可能的话,我想使用 map ,因为 map 可以在我更复杂的应用程序中使用。 如何解决此错误?
这是抛出错误时调用的 Controller 方法:
@RequestMapping(value = "/TriggerTheError", method = RequestMethod.GET)
public String processFindForm(HttpServletRequest request, BindingResult result, Map<String, Object> model) {
return "ReproduceError";
}
调用上述 Controller 方法时,控制台打印出以下日志:
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
<小时/>
回答
<小时/>解决方案是简单地删除 BindingResult,因为在这种情况下不需要它。通过从 Controller 方法的参数中删除 BindingResult,错误就被消除了。稍后,如果需要 BindingResult,则解决方案只需为 BindingResult 添加此测试用例不需要的其他所需元素。
我会将其他用户的答案标记为已接受。不过,上一段是解决问题的方案。
最佳答案
如果您打算返回字符串 ReproductiveError
,请使用 @ResponseBody
标记返回值。
另一方面,如果您有一个名为 ReproductiveError
的 View ,那么您的 View 解析器可能未正确配置?
编辑:好的,看起来像是要渲染 ReproductiveError
View ,但从日志文件来看, Controller 方法似乎没有被命中。
关于java - Spring 返回 null ModelAndView 给 DispatcherServlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29906597/