我试图通过直接写入响应对象来输出我的请求。这对 servlet 非常有效,但对于 Spring-MVC,由于某种我不明白的原因,我最终创建了一个无限循环。基本上 Controller 只是被反复调用。我什至不知道这怎么可能。
public ModelAndView handleRequest(
HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
System.out.println("I will get called infinitely");
response.getWriter().print( "Hello World!");
response.getWriter().close();
return new ModelAndView();
}
那么问题来了,有谁知道为什么会导致这个页面无限量的重新请求?它似乎只在我创建一个没有任何内容的 ModelAndView() 时发生。但在这种情况下,我不想要任何东西,只是一个空白页。那么其次有没有办法做到这一点?
最佳答案
尝试返回null
,而不是ModelAndView
,并调用flush()
而不是close()
.像这样,
public ModelAndView handleRequest( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException {
System.out.println("I will get called infinitely");
response.getWriter().print( "Hello World!");
response.getWriter().flush();
return null;
}
注意:我不确定 close()
是否会提交 response
,但 flush()
会。
关于java - Controller 在 Spring-MVC 中反复调用自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4732016/