5
votes

I use SpringBoot 1.5.9 and created src/main/resources/static/index.html:

<!DOCTYPE html><html><head><meta charset="utf-8"/>
<title>Home</title></head>
<body><h2>Hello World!!</h2></body>
</html>

My simple controller is handling "/" and forwarding to index.html:

@Controller
    public class MyController {
        @RequestMapping("/")
        public String home(Model model) {
            System.out.println("In MyController!!!");
            return "index";
    }}

However when after I run my main method from:

@SpringBootApplication
public class MySpringBoot2Application {
    public static void main(String[] args) {
        SpringApplication.run(MySpringBoot2Application.class, args);
    }
}

and I point my browser to: http://localhost:8080/ I am getting: Whitelabel Error Page-There was an unexpected error (type=Not Found, status=404)

But, if i try to access the page directly - it works fine: http://localhost:8080/index.html

Based on the SpringBoot documentation the static content under src/main/resources/static/ should be rendered. If I create folder src/main/resources/templates/ and put my index.html there, and also include spring-boot-starter-thymeleaf dependency in my pom.xml then everything works good. However I am puzzled why this basic rendering of src/main/resources/static/index.html is not working. Any help is appreciated.

2
Remove your MyController class, it should work without it - Alex Savitsky

2 Answers

4
votes

Just found the solution: the controller MyController should specify the file extension:

return "index.html";
0
votes

It is better to add viewResolverin Spring Boot configuration.

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter{

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/403").setViewName("403");
    }    


    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }    

}