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
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.
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.
index.htm
to the webapp
directory.webapp/resources
to the webapp
spring.mvc.static-path-pattern=/resources/*
/
@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.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.
Methods contactForm()
and contactSubmit()
return String
,
try to use @RestController
instead of @Controller
It fix my problem
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"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With