Issue
I don't know I have tried multiple times but never really understood how the InternalViewResolver or any view resolver work under the hood. I have configured the view resolution using WebMvcConfigurer. like this one.
@Configuration
public class WebViewConfigurer implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/register").setViewName("registration");
}
}
I have put my html file registration.html in the location: src/main/resources/templates.
Whenever I try to access the page localhost:8080/register I get the whitelabel error page
There was an unexpected error (type=Not Found, status=404).
I have configured view like this many times before and I have always faced problem in the beginning but I always get on top of it after some tweak and this time also I know I would get it. So the problem is not just how I solve my current problem but how can I stop this repetitive mistakes. Has any one faced these problems with ViewResolvers in spring or is it just me.
Solution
In case of any one wondering I solved the current problem: I added prefix and suffix in InternalViewResolved and placed the html file in target/class/resources/templates.
My current code after solving:
@Configuration
public class WebViewConfigurer implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/register").setViewName("registration");
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.viewResolver(new InternalResourceViewResolver(){{setPrefix("templates/");setSuffix(".html");}});
}
}
Answered By - xpioneer