3

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.

1
  • 3
    share your controllers and directory structure :) Commented Dec 21, 2018 at 4:21

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

It doesn't work because the files bundled into jar file. We can not get subdirectories. Hard coding also doesn't work because we have so many folders and sub folders.
If i test in Tomcat server without springboot then it works fine. Some abnormal behavior in spring boot.
@Reddy , Yes, it will bundle to a jar file which is a feature of spring boot. Doesn't mind. It should able to read files / folders inside the jar itself. I have added (edited) one another method to add resource handler. Please add those too and try again. Also debug and check whether the path of the folders are correct or not.
@Reddy , it seems the path of the folder emp is /static/emp please change my code to get the correct path and try again. It should work !
@Reddy - Was it worked for you ? If yes please select the answer as the correct answer. Let it be helpful for others too :) happy coding.
0

move your index.html page to resources/template folder, and put your css and other files in static folder

Comments

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.