spring-boot – o.s.boot.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模块.自从我做了更改后,我再也无法呈现页面了.我在模板下有index.html文件

enter image description here

我在我的控制器中添加了注释,看看它们是否被击中而且它们不是:

@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,以便有人在需要时更容易地下载和下载.

Project on 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上查看它.

我在玩你的应用程序. (git repository帮助解决了配置问题).

问题在于您的配置属性和注释的混合.
以下步骤将有助于恢复默认的Spring Boot行为.

>将index.htm移动到webapp目录.
>将所有文件从webapp / resources复制到webapp
>删除以下配置属性 – spring.mvc.static-path-pattern = / resources / *
>删除映射到/ @RequestMapping(value =“/”,method = RequestMethod.GET)和方法本身. Boot将处理index.html映射到ROOT. / mapping confused Boot Spring MVC自动配置.
>您可以完全删除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 repository.

查看有关自定义Spring MVC configuration的更多信息.

相关文章
相关标签/搜索