osboot.context.web.ErrorPage:无法转发到请求的错误页面 \[/\],因为响应已经提交

新手上路,请多包涵

当我在 tomcat 8 中呈现这个应用程序时,我一直得到一个空白页。我看到的唯一错误是:

 o.s.boot.context.web.ErrorPageFilter: Cannot forward to error page for
request [/] as the response has already been committed.

因此我的问题是我无法呈现 index.html 页面。

我拿了一个静态页面并在其中添加了一个 spring-boot 模块。自从我进行更改后,我就无法再次呈现该页面。我在 templates 下有 index.html 文件

在此处输入图像描述

我在我的控制器中添加了评论,看看它们是否被击中,而它们没有:

 @Controller
public class ApplicationController {

    @Autowired
    ContactService contactService;

    @RequestMapping(value="/", method= RequestMethod.GET)
    public String contactForm() throws IOException {
        System.out.println("Inside GET in Application Controller");

        //model.addAttribute("contact", new Contact());

        return "index";
    }

    @RequestMapping(value="/contact", method= RequestMethod.POST)
    public String contactSubmit() throws IOException {

        System.out.println("Inside POST in Application Controller");

        return "index";
    }

}

我使用的是 Thymeleaf,但决定反对它,这就是方法中的参数为空白的原因。

我已将该项目上传到 GIT,以便有人在需要时更容易四处寻找和下载。

GIT项目

——更新1———————

 @Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    private static final Logger LOGGER = Logger.getLogger(WebConfig.class);

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);

        registry.addResourceHandler("/css/**").addResourceLocations("/resources/css/");

        registry.addResourceHandler("/images/**").addResourceLocations("/resources/images/");

        registry.addResourceHandler("/image/**").addResourceLocations("/resources/image/");

        registry.addResourceHandler("/javascripts/**").addResourceLocations("/resources/javascripts/");

    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}

真的没有什么可以表明我能想到的了。如果您也愿意,也可以在 GIT 上查看其他文件。

原文由 Mike3355 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.1k
2 个回答

我在玩你的应用程序。 (git 存储库帮助解决了您的配置问题)。

问题是您的配置属性和注释的混合。以下步骤将有助于恢复 default Spring Boot 行为。

  1. 将您的 index.htm 移动到 webapp 目录。
  2. 将所有文件从 webapp/resources 复制到 webapp
  3. 去掉以下配置属性 spring.mvc.static-path-pattern=/resources/*
  4. 删除映射到 / @RequestMapping(value="/", method= RequestMethod.GET) 和方法本身。引导将处理 index.html 映射到 ROOT/ 映射混淆Boot Spring MVC自动配置。
  5. 您可以完全删除您的 WebConfig

下一步是根据需要映射静态资源:

 public class AppMvcConfiguration extends WebMvcConfigurerAdapter {
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    // Including all static resources.

    registry.addResourceHandler("/assets/**",
              "/css/**",
              "/img/**",
              "/js/**"
         ).addResourceLocations("/assets/",
              "/css/",
              "/img/",
              "/js/"
    ).resourceChain(true)
     .addResolver(new PathResourceResolver());

     super.addResourceHandlers(registry);
  }
}

查看我的 git 存储库

查看有关自定义 Spring MVC 配置 的更多信息。

原文由 Anton N 发布,翻译遵循 CC BY-SA 3.0 许可协议

方法 contactForm()contactSubmit() 返回 String

尝试使用 @RestController 而不是 @Controller

它解决了我的问题

原文由 TarikW 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进