java - Controller 在 Spring-MVC 中反复调用自身

标签 java spring spring-mvc

我试图通过直接写入响应对象来输出我的请求。这对 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/

相关文章:

java - 在 Spring Security 中添加新用户

java - 使用 Java Config 在多个 HttpSecurity 对象中自定义身份验证过滤器

java - 导入 org.springframework.test.context.junit4.SpringJUnit4ClassRunner 无法解析

java - 比 XHTMLRenderer+iText 更有效的将 HTML 转换为 PDF 的方法

java - Spring:包含bean时发生NullPointerException

java - 本地类可以访问 Java 8 中的非最终变量

java - 如何用Spring切换动态事务?

java - HttpMediaTypeNotAcceptableException : Could not find acceptable representation in exceptionhandler

java - NPE 从 AlerDialog OnClick 方法访问 Application 方法

java - 使用 StaggeredGridLayoutManager?