5

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.

1
  • Remove your MyController class, it should work without it Commented Dec 15, 2017 at 19:56

2 Answers 2

5

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

return "index.html";
Sign up to request clarification or add additional context in comments.

1 Comment

The only way i found to make it work without extension is adding to pom.xml thymeleaf dependency. If you add this dependency, it will work without extension.
0

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

}

2 Comments

He didn't mention jsp files anywhere. He even mentionned /index.html.
Its an old question but considering his question it seems like he is new to spring, So I thought to share it with him as I am sure he didn't create the spring boot project just for showing html files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.