Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

spring-boot

I keep getting a blank page when I render this application in tomcat 8. The only error I see is:

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

Therefore my issue is I am unable to render the index.html page.

I took a static page and added a spring-boot module in it. Ever since I made the change I have not been able to render the page again. I have the index.html file under templates

enter image description here

I added comments in my controller to see if they are even being hit and they are not:

@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";
    }


}

I was using Thymeleaf but decided against it which is why the parameters in the methods are blank.

I have uploaded the project to GIT to make it easier for someone to poke around and download if so desired.

Project on GIT

----------------Update 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();
    }

}

There is really nothing else to show that I can think of. You can look at it on GIT for the other files if you so choose too.

like image 624
Mike3355 Avatar asked May 15 '16 16:05

Mike3355


3 Answers

I was playing with your application. (git repository helped to solve your configuration issues).

The problem was your mix of configuration properties and annotations. The following steps will help to restore default Spring Boot behavior.

  1. Move your index.htm to the webapp directory.
  2. Copy all files from webapp/resources to the webapp
  3. Remove the following configuration property - spring.mvc.static-path-pattern=/resources/*
  4. Delete mapping to the / @RequestMapping(value="/", method= RequestMethod.GET) and the method itself. Boot will handle the index.html mapping to the ROOT. / mapping confused Boot Spring MVC auto configuration.
  5. You can delete your WebConfig completely.

The next step would be to map static resource as you want:

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);
  }
}

See my git repository.

See more information about custom Spring MVC configuration.

like image 82
Anton N Avatar answered Jan 04 '23 16:01

Anton N


Methods contactForm() and contactSubmit() return String,
try to use @RestController instead of @Controller
It fix my problem

like image 33
TarikW Avatar answered Jan 04 '23 15:01

TarikW


There may be possibility that you are missing '@ResponseBody' on your method. for an example

@RequestMapping(value = "/myMethod", method = RequestMethod.POST, headers = "Accept=application/json") 
@ResponseBody 
public String myMethod(HttpServletRequest request, HttpServletResponse response) 
{ 
    // some business logic

    retutn "Hi" 
}
like image 29
Varun Avatar answered Jan 04 '23 16:01

Varun