繁体   English   中英

控制器在Spring-MVC中反复调用自己

[英]Controller repeatedly calling itself in Spring-MVC

我试图仅通过直接写入响应对象来输出我的请求。 这对于s​​ervlet非常有效,但是对于Spring-MVC,由于某种我不了解的原因,我最终创建了一个无限循环。 基本上,控制器只是反复被调用。 我什至不知道那怎么可能。

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()是否会。

ModelAndView类的viewName可能为空,这将导致使用当前请求中的视图。

您可以使用void@ResponseBody String作为返回类型。

暂无
暂无

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

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