Reputation: 31
I am using spring boot to serve static content. I have all of my static content under src/main/resources/static
.
I have simple folder structure as below. I have no context path.
static/index.html (Default index page)
static/emp/index.html
By accessing localhost:8080
serves back the index.html
as expected.
But if access localhost:8080/emp
i am expecting the emp/index.html
be served but it is not happening. It give back error saying there is no error default error page
. It works fine in other static web servers. By default spring boot not serving index.html
under any sub folders.
Upvotes: 3
Views: 856
Reputation: 5513
It is already answered here . It's not Spring Boot mapping to index.html it's the servlet engine (it's a welcome page). There's only one welcome page (per the spec), and directory browsing is not a feature of the containers.
You can manually add a view controller mapping to make this work:
@Configuration
public class CustomWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/emp").setViewName("redirect:/emp/"); //delete these two lines if looping with directory as below
registry.addViewController("/emp/").setViewName("forward:/emp/index.html"); //delete these two lines if looping with directory as below
String[] directories = listDirectories("/");
for (String subDir : directories){
registry.addViewController(subDir).setViewName("redirect:/" + subDir + "/");
registry.addViewController(subDir).setViewName("forward:/" + subDir + "/index.html");
}
super.addViewControllers(registry);
}
}
/**
* Resources mapping
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("classpath:/assets/css/"); //my css are in src/resources/assets/css that's why given like this.
registry.addResourceHandler("/emp/**").addResourceLocations("classpath:/emp/");
registry.addResourceHandler("/images/**").addResourceLocations("classpath:/assets/images/");
registry.addResourceHandler("/js/**").addResourceLocations("classpath:/assets/js/");
}
/***
This should list subdirectories under the root directory you provide.
//Not tested
***/
private String[] listDirectories(String root){
File file = new File(root);
String[] directories = file.list(new FilenameFilter() {
@Override
public boolean accept(File current, String name) {
return new File(current, name).isDirectory();
}
});
}
The first mapping causes Spring MVC to send a redirect to the client if /emp
(without trailing slash) gets requested. This is necessary if you have relative links in /emp/index.html
. The second mapping forwards any request to /emp/
internally (without sending a redirect to the client) to the index.html
in the emp
subdirectory.
And the error message there is no default error page
you are getting because, you are trying to access localhost:8080/emp/
which is not yet configured. So spring-boot will check for any error page configured or not. As you haven't configured the error page too you are getting the above mentioned error.
Upvotes: 2
Reputation: 31
move your index.html page to resources/template folder, and put your css and other files in static folder
Upvotes: 0