10

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.

Mike3355
  • 11,305
  • 24
  • 96
  • 184
  • Do not put your index.html file to templates. it's usually in the webapp directory or served under `static` directory. See https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot , http://stackoverflow.com/questions/27583278/spring-boot-spring-mvc-thymeleaf-apache-tiles. http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html – Anton N May 15 '16 at 16:57
  • @AntonNovopashin I have tried that too. I just moved it into the static folder and it is still having the same behavior. – Mike3355 May 15 '16 at 17:35
  • Please share your configuration. Controller is not enough. Spring MVC has a couple configuration parameters `spring.mvc.view.prefix: /WEB-INF/jsp/` `spring.mvc.view.suffix: .jsp`. But usually for the static page you don't have to do that. The Boot example https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp or https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-static – Anton N May 15 '16 at 18:25
  • @AntonNovopashin please see update 1 – Mike3355 May 15 '16 at 23:20
  • Your Git hub link is no longer working. – John Aug 16 '18 at 14:45

3 Answers3

6

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

TarikW
  • 359
  • 4
  • 12
4

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.

Anton N
  • 2,317
  • 24
  • 28
4

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" 
}
sloth
  • 99,095
  • 21
  • 171
  • 219
Varun
  • 4,342
  • 19
  • 84
  • 119